Recursively Delete Empty Folders – VBScript

Ok, if you use your favourite search engine to look up a recursive deletion of empty folders, you will more than likely come up with a lot of results. I did. However by the end of the first page, I hadn’t found what I needed, so made up my own.

The special bits about this script:

  1. It forces you to use cscript and the command line, meaning that you won’t have to keep clicking OK to the prompts.
  2. It checks if there are any thumbs.db or desktop.ini files in the folders first, as these would otherwise stop the deletion.
  3. It tells you at the end if there were any errors.

Nothing particularly ground breaking, just more useful to me.

So, here it is. My advice – when running the script run it and pipe the results to a file – that way you can quickly search the file for the word ‘Error’ to see what didn’t work. For example:

This script now includes the modification suggested below by Andrew.

7 people found this post useful.

12 thoughts on “Recursively Delete Empty Folders – VBScript

  1. Hi Craig,

    the Select Case statements for removing “desktop.ini” and “thumbs.db” check vbYes twice and not vbNo!
    So you will always remove them.

    Best Regards

  2. culd you simplify it for a beginner,,,,,,lots of foldrs have turned up on my start up screen,,,dont kow where fom but ive deleted them all manually and they have comeback

    1. Hi, If the folders keep coming back then this script will not help you. You will need to find out what is repeatedly creating the folders first.

  3. 2017 and someone still need this script! :-)

    I use some of your code and some others elsewhere to make something more useful. It’s easily readable and configurable for anything else.

    The only problem is to delete multiple empty folders. This script needs to be run multiple times. In my case it’s not a problem since the script will run once a day in Task Scheduler and if there’s a few empty folders left, it’s really no big deal because the next run will delete them.

    ‘This script will delete, recursively:
    ‘ – All desktop.ini files
    ‘ – All Thumbs.db files
    ‘ – All files older than “olderThanDays”
    ‘ – All empty folders
    ‘Run it multiple times to delete multiple empty folders
    ‘Example: Script needs to be run 3 times to delete all empty folders for C:\empty1\empty2\empty3
    Option Explicit

    ‘Configurable variables
    Dim olderThanDays, folder
    olderThanDays = 365
    folder = “C:\somewhere\you\want\to\delete”

    ‘Run the script
    Dim objFSO, objFolder, objSubFolder, objFile
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    Set objFolder = objFSO.getFolder(folder)

    Call Search(objFolder)

    Sub Search(objFolder)
    For Each objFile In objFolder.files
    If dateDiff(“d”,objFile.dateLastModified,Now) > olderThanDays Then
    ElseIf strcomp(objFSO.GetFileName(objFile), “desktop.ini”) = 0 Then
    ElseIf strcomp(objFSO.GetFileName(objFile), “Thumbs.db”) = 0 Then
    End If

    For Each objSubFolder In objFolder.SubFolders
    If objSubFolder.Files.Count = 0 And objSubFolder.SubFolders.Count = 0 Then
    End If
    End Sub

    1. I’m not sure I follow what you are saying. I have just tested the copy of the script in the blog post, and it works, recursively deleting all empty folders in one go.

      Can you elaborate on the problem that you are having?

  4. Great script, just what I was looking for, thanks for sharing. I have 1 question though,
    why are you not deleting folders that start with “~”?
    Also, just a minor comment, in the script you have a comment that says ‘If the RemoveDesktopIni or RemoveThumbsDB Flag is set…… but the script does not set those 2 flags, the script will always delete these files.

    1. I exclude ~ folders as they are snapshot directorys on our storage system, so are not writeable.

      I have also now added in the missing flags and questions to the functions. Thanks for pointing that out.

  5. A little late to the game I know. First off I want to say this is a nice script BUT it could be a little better. Say I have the following structure
    -folder a
    —folder a1
    —folder a2

    Currently your code will delete folder a1 and a2, BUT leave folder a, even though it will eventually become empty. If you move the subdirectory recursive call to before the file.count it will process all the subfolders first (A depth first search) and in this case it will delete a1,a2 then a!! here is the change. The code is nearly identical, I just moved some things around

    ‘ check if there are any subfolders.
    If objFolder.SubFolders.Count 0 Then
    ‘Subfolders found, so go through each of those.
    For Each objSubFolder in objFolder.SubFolders
    RecursiveDeleteEmptyFolders objSubFolder.Path
    End If

    ‘Now check if the folder contains any files.
    If objFolder.Files.Count = 0 Then
    if objFolder.SubFolders.Count = 0 Then
    ‘Check that the subfolders do not begin with a tilde, and if they do not, delete the folder.
    If Left(objFolder.Name,1) “~” Then
    WScript.Echo “Deleting: ” & objFolder.Path
    If Err Then
    WScript.Echo “Error deleting:” & objFolder.Name & ” – ” & Err.Description
    intErrorCount = intErrorCount + 1
    End If
    End If
    end if

    1. Hi Andrew,

      You are totally right, and it turns out I had changed the copy of the script on my computer to basically what you had done and never updated the copy on my site! Thanks for the feedback.

    2. Andrew, your modification is a good one. If your intention was to instruct you succeeded.
      However, if your intention was to make this script useful for others, you might have provided the full routine rather than just the snippet which illustrates the logic. That way people wouldn’t have had to cut and paste into the original risking syntax errors.

      1. Thanks Chris, you are right in what you say. I would hope that people would find the time to learn the language and be able to fix the errors, but for something utilitarian such as this I can see that you would just want to copy and paste and it would work.

        Based on that, I have updated the main code in the post and credited Andrew with the change.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.