Empty Folder Search...Need to Add Deeper Folder Levels to VBA Coding

Arkabama

New Member
Joined
Mar 22, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am attempting to construct a macro to identify empty folders within a directory and have been somewhat successful. In the code below, I can run the macro and select a folder with a dialog box, and it creates a link in my spreadsheet starting in Cell A5. It will identify the subfolders in the object folder that do not contain files, but it only lists the subfolder, not any nested folders within. Does anyone have thoughts on how I can add code that will achieve this? Thank you fellow VBAers.

Sub ListEmptyFolders()

Dim xPath As String
Dim objFSO As Object
Dim objFolder As Object
Dim ObjSubFolder As Object
Dim i As Integer
Dim Folder As String
Dim FriendlyName As String
Dim CheckFolder As String

With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose the folder"
.Show
End With
On Error Resume Next

xPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(xPath)
i = 1
For Each ObjSubFolder In objFolder.SubFolders
If ObjSubFolder.Size = 0 Then
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i + 4, 1), _ 'starts list in Cell A5
Address:=ObjSubFolder, _
TextToDisplay:=objFolder + ObjSubFolder.Name
CheckFolder = ObjSubFolder & ""
i = i + 1
End If
Next ObjSubFolder
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
This is not something I do often (OK it is actually rare). Unless somebody feeds you code, what you want to look for is a recursive function. Such a procedure will drill down in a folder's subfolders until if finds no more. Then it will execute the next iteration one level up until it finds no more. Then it will execute the next iteration one level up ... until it gets back to the starting folder. If there are no more subfolders in the starting folder, the code is done. Otherwise it moves down one sub folder and sort of starts all over again.

Please post code tags (use vba button on posting toolbar) and paste between the resulting vba code tags.
 
Upvote 1
Solution
VBA Code:
Sub ListEmptyFolders()

Dim xPath As String
Dim objFSO As Object
Dim objFolder As Object
Dim ObjSubFolder As Object
Dim i As Integer
Dim Folder As String
Dim FriendlyName As String
Dim CheckFolder As String

With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose the folder"
.Show
End With
On Error Resume Next

xPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(xPath)
i = 1
For Each ObjSubFolder In objFolder.SubFolders
If ObjSubFolder.Size = 0 Then
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i + 4, 1), _ 'starts list in Cell A5
Address:=ObjSubFolder, _
TextToDisplay:=objFolder + ObjSubFolder.Name
CheckFolder = ObjSubFolder & ""
i = i + 1
End If
Next ObjSubFolder
End Sub
 
Upvote 0
VBA Code:
Sub ListEmptyFolders()

Dim xPath As String
Dim objFSO As Object
Dim objFolder As Object
Dim ObjSubFolder As Object
Dim i As Integer
Dim Folder As String
Dim FriendlyName As String
Dim CheckFolder As String

With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose the folder"
.Show
End With
On Error Resume Next

xPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(xPath)
i = 1
For Each ObjSubFolder In objFolder.SubFolders
If ObjSubFolder.Size = 0 Then
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i + 4, 1), _ 'starts list in Cell A5
Address:=ObjSubFolder, _
TextToDisplay:=objFolder + ObjSubFolder.Name
CheckFolder = ObjSubFolder & ""
i = i + 1
End If
Next ObjSubFolder
End Sub
Ok, figured the code tag out. Thanks for the tip.
 
Upvote 0
Just need to work on your indentation now. Also see comment I added in code.
VBA Code:
Sub ListEmptyFolders()
Dim xPath As String, Folder As String, FriendlyName As String, CheckFolder As String
Dim objFSO As Object, objFolder As Object, ObjSubFolder As Object
Dim i As Integer

With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Choose the folder"
    .Show
End With
On Error Resume Next '?? If err is raised in next line, it will be ignored. Could be an issue

xPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(xPath)
i = 1
For Each ObjSubFolder In objFolder.SubFolders
    If ObjSubFolder.Size = 0 Then
        ActiveSheet.Hyperlinks.Add Anchor:=Cells(i + 4, 1), _ 'starts list in Cell A5
        Address:=ObjSubFolder, _
        TextToDisplay:=objFolder + ObjSubFolder.Name
        CheckFolder = ObjSubFolder & ""
        i = i + 1
    End If
Next ObjSubFolder
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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