Use VBScript to Silently Remove Empty Folders

Scott FellDeveloper & Coffee Roaster
CERTIFIED EXPERT
I have over 30 years experience in Sales, Marketing, Technology and Developing solutions.
Published:
Edited by: Andrew Leniart
This code will silently delete all empty folders in the given start folder using VBScript.

I decided to write this article after I saw an interesting question here on Experts Exchange by one of our new members, asking how to delete empty folders and subfolders in a directory without any prompts.


The script is well commented but I am going to outline the major portion of the code here.


  1. Set the “Start Folder” - this will be the folder where you want to clean up all empty folders.  My example uses "C:\temp\ee\clean"

  2. VBScript does not have very good array support so we have to use some tricks. I will be creating a collection of found folders and saving to a text field that will get delimited with a special delimiter that will not get confused with other possible characters in a folder name.  The field "txtFolderList" will hold the text list.

  3. Here will run a subroutine that will recursively find all subfolders in our start folder. As folders are found, the folder names will be saved in "txtFolderList"

  4. Here we are creating an array from our text field.  We will use the array to loop through all the found folders.

  5. Finally, we call a subroutine for each folder in the array that will check if the folder is empty. If the folder is empty, it will get deleted. Note that we are starting with the last array object and going towards the first. The reason is the first folders will be filled with the last folders and will not be found as empty. By working backward, we check if the last item is empty and if it is, delete it. This allows the parent folders to be deleted if no other files are found.

The subroutines are well commented. The script will run wherever you can run VBScript (Windows). To run the script, save the file with the .vbs extension. From there you can use the command prompt or anything that can call a command line.  


Assuming the file is called "clean.vbs" and is found in c:\scripts\clean.vbs, you only need to enter c:\scripts\clean.vbs to the command prompt or call the line in your own code.


' *************************************
'  Author: Scott Fell 
'  https://www.experts-exchange.com/members/padas.html
'  Date: January 2020
'  Original Posted: Experts-Exchange.com
'  ************************************
'  WARNING! THIS ROUTINE WILL DELETE ALL SUBFOLDERS IN THE START FOLDER IF THE FOLDER IS EMPTY
'  USE THIS CODE AT YOUR OWN RISK
'  PLEASE MAKE SURE TO TEST BEFORE USING LIVE
' *************************************

' 1) THE FOLDER TO START
startFolder = "C:\temp\ee\empty1"

' 2) VARIABLE TO HOLD ALL FOUND FOLDERS
txtFolderList = ""

' CREATE FILE SYSTEM OBJECT
Set fso = CreateObject("Scripting.FileSystemObject")

' 3) CALL SUBROUTINE THAT WILL RECURSIVELY GET ALL SUB FOLDERS
getSubfolders fso.GetFolder(startFolder)

' WHEN txtFolderList IS POPULATED
' IT WILL LOOK LIKE c:\path\folder[|]c:\path\folder[|]c:\path\folder
' 4) CREATE AN ARRAY USING splut and [|] TO SEPARATE

arrSubFolders = split(txtFolderList,"[|]")


' 5) LOOP THROUGH ARRAY BACKWARDS
' ALLOWS TO DELETE THE LAST FOLDER FIRST
intArraySize = LBound(arrSubFolders) 'array size
For x = UBound(arrSubFolders) to 0 step -1 'LOOP THROUGH FOLDERS BACKWARDS
	'RUN THE CLEAN ROUTINE FOR THE CURRENT SUBFOLDER
	clean arrSubFolders(x)
Next

'Wscript.Echo txtFolderList


' SUBROUTINE TO RECURSIVELY GET ALL SUB FOLDERS
Sub getSubfolders(Folder)

	'LOOP THROUGH ALL SUB FOLDERS
	For Each Subfolder in Folder.SubFolders
	
		if txtFolderList = "" then 'IF THIS IS THE FIRST ENTRY
			
			txtFolderList = Subfolder.Path 'ADD THE FIRST SUB FOLDER TO OUR txtFolderList
			
			else
			
			' CONTINUE TO ADD TO THE FIELD txtFolderList USING A DELIMINTER OF [|]
			txtFolderList = txtFolderList &"[|]"& Subfolder.Path
			
		end if
	
		'CALL getSubfolders INSIDE THE LOOP TO GET MORE SUB FOLDERS
		getSubfolders Subfolder
		
	Next
End Sub

' SUB ROUTIE TO CHECK IF FOLDER IS EMPTY
' DELETE FOLDER IF IT IS EMPTY
sub clean(strFolder)

	If fso.FolderExists(strFolder) Then 'MAKE SURE FOLDER EXISTS
	
		Set objFolder = fso.GetFolder(strFolder) 'CREATE FOLDER OBJECT
	  
		' CHECK THAT FOLDER IS EMPTY (REASON WE ARE GOING BACKWARDS)
		If objFolder.Files.Count = 0 And objFolder.SubFolders.Count = 0 Then 
		
			'DELETE FOLDER WHEN TRUE (EMPTY)
			fso.DeleteFolder strFolder
			
			
		end if
		
	End If

end sub


set fso = nothing


3
3,371 Views
Scott FellDeveloper & Coffee Roaster
CERTIFIED EXPERT
I have over 30 years experience in Sales, Marketing, Technology and Developing solutions.

Comments (2)

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Distinguished Expert 2020

Commented:
A slightly simpler approach...

Const strBaseDir = "C:\Temp"

set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

RemoveFolder objFSO.GetFolder(objFSO.GetAbsolutePathname(strBaseDir))

Sub RemoveFolder(objFolder)
    For Each objSubFolder In objFolder.Subfolders
        RemoveFolder objSubFolder
    Next
    If objFolder.Files.Count = 0 And objFolder.Subfolders.Count = 0 Then
        objFolder.Delete
    End If
End Sub

Open in new window


»bp
Scott FellDeveloper & Coffee Roaster
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Author

Commented:
Thank you Bill. I tend to write functions I can re use out of habit and loose sight of simplification.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.