Given folder path in Column A, get file count of all sub-folders and files and put that number in Column B

humerick

New Member
Joined
Oct 8, 2009
Messages
6
Path# of files in folder/subfolders
Z:\Windows\User139
Z:\System\files1213
Z:\Windows\Temp5600
Z:\Data\user\jamie\files125

<tbody>
</tbody>


Can someone help me with some VBA code that will read down through Column A and starting at the path in that cell, get a count of all files and sub-folders under the path and report that number in Column B?

Thank you in advance for any help on this!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
<b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: rgb(255,255,255);border-collapse: collapse; border-color: rgb(187,187,187)"><thead><tr style=" background-color: rgb(218,231,245);color: rgb(22,17,32)"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: rgb(218,231,245);color: rgb(22,17,32)">B1</th><td style="text-align:left">=Count_All_Files(<font color="Blue">A1</font>)</td></tr></tbody></table></td></tr></table><br />


Code:
Dim objFso As Object
Function Count_All_Files(ByVal sPath As String) as long 
Dim objFldr As Object
If objFso Is Nothing Then Set objFso = CreateObject("Scripting.FileSystemObject")
Count_All_Files = objFso.GetFolder(sPath).Files.Count
For Each objFldr In objFso.GetFolder(sPath).SubFolders
    Count_All_Files = Count_All_Files + Count_All_Files(objFldr.Path)
Next objFldr
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,737
Messages
6,126,562
Members
449,318
Latest member
Son Raphon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top