Macro to list folders and subfolders in excel based explorer with Properties

JSH720

Board Regular
Joined
Oct 9, 2009
Messages
109
Office Version
  1. 365
Platform
  1. Windows
I need a macro that , in excel, shows a folder name, its path, # folders underneath it and # files in each folder. Each sub folder would also be listed and properties included in the spreadsheet. Like I was clicking on properties of each folder and sub folder individually (which I don't want to do since there are thousands of them and thousands of sub folders). CMD way does not work since it is not in excel format and really difficult to parse.

Id like to be able to click on the folder of my choice to start this process instead of copying and pasting its path.

Thank YOu!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Paste this into a new module in the VBA editor:

Code:
Dim fso As Object
Dim nextRow As Long
Dim indent As Long
Dim newSheet As Worksheet
Public Sub ShowFolderDetails()

Set fso = CreateObject("Scripting.FileSystemObject")
Set newSheet = Sheets.Add(after:=Sheets(Sheets.Count))
newSheet.Range("A1").Value = "Name"
newSheet.Range("B1").Value = "Path"
newSheet.Range("C1").Value = "Subfolders"
newSheet.Range("D1").Value = "Files"
newSheet.Range("1:1").Font.Bold = True

nextRow = 2
indent = 1

With Application.FileDialog(msoFileDialogFolderPicker)
    .InitialFileName = Environ("USERPROFILE")
    .Show
    RecurseFolder .SelectedItems(1)
End With

newSheet.Columns("A:D").EntireColumn.AutoFit

End Sub
Private Sub RecurseFolder(folderPath As String)

Dim folderCount As Long
Dim fileCount As Long
Dim subFolder As Folder
Dim thisFolder As Folder

Set thisFolder = fso.GetFolder(folderPath)

folderCount = thisFolder.SubFolders.Count
fileCount = thisFolder.Files.Count
newSheet.Cells(nextRow, 1).Value = thisFolder.Name
newSheet.Cells(nextRow, 2).Value = folderPath
newSheet.Cells(nextRow, 3).Value = folderCount
newSheet.Cells(nextRow, 4).Value = fileCount
indent = indent + 1
nextRow = nextRow + 1
For Each subFolder In thisFolder.SubFolders
    RecurseFolder subFolder.Path
Next subFolder
indent = indent - 1

End Sub

Run the ShowFolderDetails macro.

WBD
 
Upvote 0
Paste this into a new module in the VBA editor:

Code:
Dim fso As Object
Dim nextRow As Long
Dim indent As Long
Dim newSheet As Worksheet
Public Sub ShowFolderDetails()

Set fso = CreateObject("Scripting.FileSystemObject")
Set newSheet = Sheets.Add(after:=Sheets(Sheets.Count))
newSheet.Range("A1").Value = "Name"
newSheet.Range("B1").Value = "Path"
newSheet.Range("C1").Value = "Subfolders"
newSheet.Range("D1").Value = "Files"
newSheet.Range("1:1").Font.Bold = True

nextRow = 2
indent = 1

With Application.FileDialog(msoFileDialogFolderPicker)
    .InitialFileName = Environ("USERPROFILE")
    .Show
    RecurseFolder .SelectedItems(1)
End With

newSheet.Columns("A:D").EntireColumn.AutoFit

End Sub
Private Sub RecurseFolder(folderPath As String)

Dim folderCount As Long
Dim fileCount As Long
Dim subFolder As Folder
Dim thisFolder As Folder

Set thisFolder = fso.GetFolder(folderPath)

folderCount = thisFolder.SubFolders.Count
fileCount = thisFolder.Files.Count
newSheet.Cells(nextRow, 1).Value = thisFolder.Name
newSheet.Cells(nextRow, 2).Value = folderPath
newSheet.Cells(nextRow, 3).Value = folderCount
newSheet.Cells(nextRow, 4).Value = fileCount
indent = indent + 1
nextRow = nextRow + 1
For Each subFolder In thisFolder.SubFolders
    RecurseFolder subFolder.Path
Next subFolder
indent = indent - 1

End Sub

Run the ShowFolderDetails macro.

WBD
I know I'm doing something wrong (novice at VBA) It errors out at "private Sub RecurseFoler (folderpath as string)

What am I missing?
 
Upvote 0
I tried it and had the same error, it's on the "...as Folder" dim. Perhaps WBD is using an add-in/extension we don't have loaded?
 
Upvote 0
I think you need to set a reference to "Microsoft Scripting RunTime"
 
Upvote 0
Oops. Shoddy work there. I did create it with a reference to Microsoft Scripting Runtime and made a poor effort at removing that dependency. Tsk tsk. This will work (on Windows) without adding the reference:

Code:
Dim fso As Object
Dim nextRow As Long
Dim indent As Long
Dim newSheet As Worksheet
Public Sub ShowFolderDetails()

Set fso = CreateObject("Scripting.FileSystemObject")
Set newSheet = Sheets.Add(after:=Sheets(Sheets.Count))
newSheet.Range("A1").Value = "Name"
newSheet.Range("B1").Value = "Path"
newSheet.Range("C1").Value = "Subfolders"
newSheet.Range("D1").Value = "Files"
newSheet.Range("1:1").Font.Bold = True

nextRow = 2
indent = 1

With Application.FileDialog(msoFileDialogFolderPicker)
    .InitialFileName = Environ("USERPROFILE")
    .Show
    RecurseFolder .SelectedItems(1)
End With

newSheet.Columns("A:D").EntireColumn.AutoFit

End Sub
Private Sub RecurseFolder(folderPath As String)

Dim folderCount As Long
Dim fileCount As Long
Dim subFolder As Object
Dim thisFolder As Object

Set thisFolder = fso.GetFolder(folderPath)

folderCount = thisFolder.SubFolders.Count
fileCount = thisFolder.Files.Count
newSheet.Cells(nextRow, 1).Value = thisFolder.Name
newSheet.Cells(nextRow, 2).Value = folderPath
newSheet.Cells(nextRow, 3).Value = folderCount
newSheet.Cells(nextRow, 4).Value = fileCount
indent = indent + 1
nextRow = nextRow + 1
For Each subFolder In thisFolder.SubFolders
    RecurseFolder subFolder.Path
Next subFolder
indent = indent - 1

End Sub

On a Mac it's a no go ...

WBD
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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