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
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Path[/TD]
[TD]# of files in folder/subfolders[/TD]
[/TR]
[TR]
[TD]Z:\Windows\User[/TD]
[TD]139[/TD]
[/TR]
[TR]
[TD]Z:\System\files[/TD]
[TD]1213[/TD]
[/TR]
[TR]
[TD]Z:\Windows\Temp[/TD]
[TD]5600[/TD]
[/TR]
[TR]
[TD]Z:\Data\user\jamie\files[/TD]
[TD]125[/TD]
[/TR]
</tbody>[/TABLE]


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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
<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,223,958
Messages
6,175,629
Members
452,661
Latest member
Nonhle

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