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.
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
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.
Comments (2)
Commented:
Open in new window
»bp
Author
Commented: