Deleting empty subdirectories seems like something Windows ought to do automatically, but for some reason that function just isn't included as part of the OS. Luckily, you don't need to download a utility to do it; instead, you can create a single line batch file that will do exactly what you're looking for:
for /f "usebackq" %%d in ("dir /ad/b/s | sort /R") do rd "%%d"
Raymond Chen, the venerable Microsoft coder, explains how this works. Basically, it uses a trick to enumerate the directories in reverse order. Since you can't delete a directory that contains data (including other directories) the only way to automate deleting directories is to start at the very bottom of the tree and work your way up.














Reader Comments (Page 1 of 1)
5-05-2008 @ 1:26PM
Jeebus said...
RD /S works for me.
Reply
5-06-2008 @ 1:05AM
hazard said...
I presume this is for a deltree operation that works on all flavours of Windows cause it's certainly not required for WinXP as Jeebus has pointed out rd /s or rmdir /s do this already ...
Reply
5-06-2008 @ 11:29AM
Mark said...
Uh guys, doing that will delete everything. Files and all. This is just to delete empty subdirectories and leave your files alone.
5-07-2008 @ 11:26PM
dsfdsfsdf said...
"Deleting empty subdirectories seems like something Windows ought to do automatically"
-> Huh? I'm pretty glad that it does not...
Reply