Batch script to delete all empty subdirectories.
I needed a script to delete all empty subdirectories, but the examples I found failed with directories with spaces in them, or would not let me pass the directory name to the script.
This bat file will do the trick.
@echo off
for /f "delims=" %%i in ('dir "%~f1" /ad /b /s ^| sort /R') do rd "%%i" 1>NUL 2>&1
The batch file will list all directories, in reverse order, and then attempt to delete each one.
rd will only delete empty directories.
The delims parameter stops spaces from being used as a delimiter.
%~f1 lets you pass a directory name to the script.
We use IO redirection at the end of the script to prevent warnings from being displayed on screen.
1>NUL 2>&1
This code says to redirect standard output (handle 1) to NUL, and also take sderr (handle 2) and redirect that to handle 1.
Thanks to Raymond Chen for the original code.