A couple of notes...
1. This script was designed to run from the command line in the directory where you want the search for empty directories to begin. You can't just double-click on the script and expect it to work the way you want it to. The script could easily be modified to request the starting folder. Since I use this routine infrequently, this enhancement would just slow me down, so... I didn't add it.
2. This routine will not delete an empty directory if a program or process has a hold on it. Windows Explorer does this every once in a while.
3. Directories that only contain empty directories are also deleted.
4. Not all empty directories are evil. Some are required. Please, I'm begging you, please use this script with care.
5. This script contains a bunch of code to display splash messages. Feel free to delete it. You don't really need it. I kept it in because it's a nice-to-have when dealing with large folder structures.
Here's the code. I hope you find it useful:
;RemoveEmptyDirectories.ahk ;-- Confirm MsgBox 1,,About to remove all empty directories from:`r`n%A_WorkingDir%\ IfMsgBox Cancel return ;-- Initialize $MinimumSplashTime=850 ;[========================] ;[ ] ;[ Build Directory List ] ;[ ] ;[========================] ;-- Splash SplashImage,, W258 B2 ,,Building directory list... $SplashStartTime=%A_TickCount% Sleep 10 ;-- Allow splash image to render ;-- Build directory list $DirectoryList= Loop *.*,2,1 $DirectoryList=%$DirectoryList%%A_LoopFileFullPath%`r`n ;-- Sort it Sort $DirectoryList ;-- Compute elapsed time. Sleep if necessary $SplashElapsedTime:=A_TickCount - $SplashStartTime if $SplashElapsedTime<%$MinimumSplashTime% { $SleepTime:=$MinimumSplashTime - $SplashElapsedTime Sleep %$SleepTime% } ;[==================] ;[ ] ;[ Remove Empties ] ;[ ] ;[==================] ;-- Splash SplashImage,, W354 B2 ,,Searching for directories to delete... $SplashStartTime=%A_TickCount% ;--- Loop until all empty directories have been deleted $DeleteTotal=0 $LoopCount=0 Loop 99 { $LoopCount:=$LoopCount + 1 $DeleteCount=0 Loop parse,$DirectoryList,`n,`r { if A_LoopField= ;-- Ignore blank item continue IfExist %A_LoopField% { FileRemoveDir %A_LoopField% if errorlevel=0 $DeleteCount:=$DeleteCount + 1 } } $DeleteTotal:=$DeleteTotal + $DeleteCount If $DeleteCount=0 break } ;-- Compute elapsed time. Sleep if necessary $SplashElapsedTime:=A_TickCount - $SplashStartTime if $SplashElapsedTime<%$MinimumSplashTime% { $SleepTime:=$MinimumSplashTime - $SplashElapsedTime Sleep %$SleepTime% } ;-- Splash off SplashImage off ;[===========] ;[ ] ;[ Results ] ;[ ] ;[===========] If $DeleteTotal=0 msgbox No empty directories found else msgbox %$DeleteTotal% empty directories removed ;-- Return to sender return