On Error Resume Next '------------------------------------------------------------------------------------------------------------------- 'Create a Wscript Shell Object in order to communicate with the user. Set objWShell = Wscript.CreateObject ("Wscript.Shell") 'Create a File System Object in order to manipulate path names. Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject") '------------------------------------------------------------------------------------------------------------------- If WScript.Arguments.Count = 0 Then WScript.Echo "Specify the folder to begin search." WScript.Quit 255 End If If Not objFSO.FolderExists(WScript.Arguments(0)) Then WScript.Echo "The given folder does not exist." & vbLf & vbLf & WScript.Arguments(0) WScript.Quit 254 End If '------------------------------------------------------------------------------------------------------------------- intResult = 0 intElement = 0 intCountFoldersAll = 0 intCountFoldersRemoved = 0 ReDim arrFolders(-1) '------------------------------------------------------------------------------------------------------------------- Set objFolder = objFSO.GetFolder(WScript.Arguments(0)) udsGetFolders(objFolder) For intElement = UBound(arrFolders) To 0 Step -1 Set objFolder = objFSO.GetFolder(arrFolders(intElement)) If objFolder.SubFolders.Count = 0 And objFolder.Files.Count = 0 Then udsDisplayMessage1() If intresult = vbCancel Then WScript.Quit 253 'Quick way out. objFolder.Delete intCountFoldersRemoved = intCountFoldersRemoved + 1 End If Next udsDisplayMessage2() '------------------------------------------------------------------------------------------------------------------- 'Destroy the FSO Object.' Set objFSO = Nothing 'Destroy the WShell Object. Set objWShell = Nothing WScript.Quit 0 'End of main script. '=================================================================================================================== 'Subs and Functions. '=================================================================================================================== Sub udsGetFolders(objFolder) For Each SubFolder in objFolder.SubFolders Redim Preserve arrFolders(UBound(arrFolders) + 1) arrFolders(UBound(arrFolders)) = SubFolder intCountFoldersAll = intCountFoldersAll + 1 Next For Each objSubFolder in objFolder.SubFolders udsGetFolders objSubFolder Next End Sub '-------------------------------------------------------------------------------------------------------------------' Sub udsDisplayMessage1() 'Display status information. strMsgTitle = "VBS Info" & "|" & WScript.ScriptFullName strMsgText = "Folders total:{t}{1}{n}Folders removed:{t}{2}{n}{n}Next folder to be removed ...{n}{n}{3}{n}{s}{n}{n}" strMsgText = Replace(strMsgText,"{1}",intCountFoldersAll,1,1,0) strMsgText = Replace(strMsgText,"{2}",intCountFoldersRemoved,1,1,0) strMsgText = Replace(strMsgText,"{3}",arrFolders(intE),1,1,0) strMsgText = Replace(strMsgText,"{t}",vbTab,1,-1,0) strMsgText = Replace(strMsgText,"{n}",vbLf,1,-1,0) strMsgText = Replace(strMsgText,"{s}",String(120,Chr(160)),1,1,0) 'Crude sizing the message box. intSecondsToWait = 2 intButton = vbOkCancel + vbInformation + vbDefaultButton2 intResult = objWShell.Popup(strMsgText, intSecondsToWait, strMsgTitle, intButton) End Sub '-------------------------------------------------------------------------------------------------------------------' Sub udsDisplayMessage2() 'Display ready message. strMsgTitle = "VBS Info" & "|" & WScript.ScriptFullName strMsgText = "Folders total:{t}{1}{n}Folders removed:{t}{2}{n}{s}{n}{n}" strMsgText = Replace(strMsgText,"{1}",intCountFoldersAll,1,1,0) strMsgText = Replace(strMsgText,"{2}",intCountFoldersRemoved,1,1,0) strMsgText = Replace(strMsgText,"{t}",vbTab,1,-1,0) strMsgText = Replace(strMsgText,"{n}",vbLf,1,-1,0) strMsgText = Replace(strMsgText,"{s}",String(40,Chr(160)),1,1,0) 'Crude sizing the message box. intSecondsToWait = 0 intButton = vbOkOnly + vbInformation intResult = objWShell.Popup(strMsgText, intSecondsToWait, strMsgTitle, intButton) End Sub '=================================================================================================================== 'End of VBS script. '-------------------------------------------------------------------------------------------------------------------' 'Invoke this script from the Windows/DOS commandline. 'Or use a fully qualified comandline like ... "WScript.exe" "RemoveEmptyFolders.vbs" "Folderpath Name" '(c)20131101.Detlev Dalitz. '===================================================================================================================