A better way to delete empty sub-folders in a directory tree.

Jerold Schulman

August 16, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today

In tip 8300, I scripted DelMT.bat to delete empty sub-folders in a directory tree.

Jim Long scripted DelMT.bat, making use of the SORT /Reverse command and suppressing errors, resulting in a much simpler solution.

DelMT.bat contains:

@echo offif {%1}=={} @echo Syntax DelMT Folder&goto :EOFif not exist %1 @echo Syntax DelMT Folder - %1 not found.&goto :EOFsetlocalset Folder=%1:: REMOVE EMPTY SUBFOLDERSfor /f "tokens=*" %%A in ('dir /ad /s /b %Folder% ^|Sort /Reverse') do ( RmDir "%%A" 2>NUL):: REMOVE FOLDER, IF EMPTYRmDir %Folder% 2>NULendlocal


Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like