delete empty folders in a drive

VinodN

New Member
Joined
Aug 31, 2015
Messages
18
Hi,

I ran a macro that have created some 1700 odd folders in the main folder. Out of these some folders contain pdf files and some are empty. I need a macro to delete these empty folders.

I tried the below script but nothing seems to be working;
Sub G_Delete_Folder()
'On Error Resume Next
RmDir "S:\profile\3 Payroll\2016-2017\1. Wages\1. Monthly\Payroll" (the folders to be deleted are in 'Payroll' folder)
On Error GoTo 0
MsgBox "All empty folders have been deleted!!"
End Sub

Not sure where I am going wrong. Can someone please help.

Regards,
Vinod
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hello Vinod, I was able to get the code below to work for me. I deleted some test folders that had the same name except for the last number, which was in sequential order. You'll need to adapt the conditions that you use to get the folder names, but as long as there is some way for the loop to identify each folder, it will count the files in the folder and delete it if there are no files in it. You'll need to make sure to check the Microsoft Scripting Runtime option from the Tools --> References menu in the VBA window for this to work.

Code:
Sub DeleteEmptyFolders()
    Dim fso As Scripting.FileSystemObject, fil As Scripting.File, CheckFolder As Scripting.Folder, FolderPath$, i%
    
    Set fso = New Scripting.FileSystemObject
[COLOR=#008000]    'Where I have "i = 1 To 100", you'll need to change that to some reasonable way for VBA to find the name of the next folder, probably something having to do with dates.
    'If you provide a sample of some of the folder names that you have, I might be able to help you find a way to adjust this code so it will access your specific folders.[/COLOR]
    For i = 1 To 100
        FolderPath = "S:\profile\3 Payroll\2016-2017\1. Wages\1. Monthly\Payroll" & i
        Set CheckFolder = fso.GetFolder(FolderPath)
        If CheckFolder.Files.Count = 0 Then CheckFolder.Delete
    Next i
    MsgBox "All empty folders have been deleted!!"
End Sub

I didn't include any error handling in this code since that's usually very specific to each situation. I'd need some more information, especially folder names, in order to do any work with that.
 
Upvote 0
I was curious so I did some additional research and came up with this method of deleting empty folders. It "SHOULD" work regardless of file name. Again, make sure that you have enabled the Microsoft Scripting Runtime option from the Tools --> References menu in order for this to work.

Code:
Sub DeleteEmptyFolders()
    Dim fso As Scripting.FileSystemObject, MainFolder As Scripting.Folder, CheckFolder As Scripting.Folder, MainFolderPath$, FolderPath$
    
    Set fso = New Scripting.FileSystemObject
    MainFolderPath = "S:\profile\3 Payroll\2016-2017\1. Wages\1. Monthly\Payroll"
    Set MainFolder = fso.GetFolder(MainFolderPath)
    
    For Each CheckFolder In MainFolder.SubFolders
        FolderPath = MainFolderPath & "\" & CheckFolder.Name
        Set CheckFolder = fso.GetFolder(FolderPath)
        If CheckFolder.Files.Count = 0 Then CheckFolder.Delete
    Next CheckFolder
    MsgBox "All empty folders have been deleted!!"
End Sub

As with all code, be careful when executing this. Especially since it is accessing your file directories, I'd step through it for the first few iterations of the loop before allowing it to finish, just to make sure it's working as intended. You can use the Locals window to help figure out what it is setting the file path strings to. Good luck!
 
Upvote 0
I was curious so I did some additional research and came up with this method of deleting empty folders. It "SHOULD" work regardless of file name. Again, make sure that you have enabled the Microsoft Scripting Runtime option from the Tools --> References menu in order for this to work.
........!

Many thanks for the code, but a warning with the methodology above.

This will delete a folder without any files in it even if it contains a subfolder that contains files. Therefore folders with files in them with parent directories without files will be deleted.

I'm not sure if there is a method to count folders rather than just files, which would overcome this issue.

I demonstrated this in a test environment and so no files were harmed in the making of this test! If a find a solution before anyone else I will update the thread.
 
Upvote 0
I changed CheckFolder.Files.Count = 0 to CheckFolder.size = 0 and that worked for me.

 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,101
Members
449,066
Latest member
Andyg666

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