I have a directory on my Windows 7 machine that has hundreds if not thousands of sub-directories. Some of them have files, some do not. I want to delete all the empty directories.

Looking at the del and rmdir DOS command, it does not look like you can recursively do this without deleting all the files. Is there a way to do this from the command line? Or is there a tool that would do it for me?

link|improve this question
I am amazed there's no simple answer to this question. – billpg Apr 5 at 12:45
feedback

6 Answers

up vote 22 down vote accepted

You can use this utility: Remove Empty Directories

Alternatively you can use this one-liner batch file:

for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do rd "%%d"

One-liner taken from DownloadSquad, an excellent site to add to your RSS feeds. :)

link|improve this answer
4  
P.S I suggest you try the GUI-based tool first, before trying any command-line commands that can potentially delete all files. – caliban Sep 11 '09 at 13:50
that tool looks good. I will check it out and report back – mohlsen Sep 11 '09 at 14:31
+1 for recommending DownloadSquad. – alex Sep 11 '09 at 14:31
Using the batch version gives me an error: The system cannot find the file dir /ad/b/s | sort /R. – EBGreen Sep 11 '09 at 15:58
Hang on, testing command in my Windows VM. – caliban Sep 11 '09 at 16:02
show 7 more comments
feedback

The free utility EmptyFolderNuker does this fine, from a base folder of your choice. It also removes those directories only containing empty sub-directories.

link|improve this answer
The one-liner batch file didn't work for me (I get the error "%%d was unexpected at this time."), and the Remove Empty Directories program will not install (it says on the linked page that it is not compatible with Windows 7 anyway). This program worked like a charm. – Phoenix Mar 7 '10 at 11:52
feedback

If you get the get the error:

"%%d was unexpected at this time.

it is likely you are running directly from the command line. In that case, change the double %% to a single %:

for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"
link|improve this answer
feedback

The excellent Glary Utilities has this and a bunch of other great features.

link|improve this answer
feedback

Since Cygwin comes with GNU find, you can do this:

find . -type d -empty -delete

Or to avoid the noise when a folder no longer exists:

find . -type d -empty -execdir rmdir {} +
link|improve this answer
feedback

If you have Cygwin installed, you could do this:

find -type d -exec rmdir {} \;
link|improve this answer
2  
Not necessarily. That might not delete directories with only empty subdirectories. You might have to reverse it. find -type d -print0 | tac | xargs -0 rmdir – Ryan Thompson Sep 11 '09 at 22:56
I never knew about "tac" before. That's really nifty! – Nighthawk Sep 12 '09 at 1:52
feedback

protected by nhinkle Jun 19 '11 at 22:58

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.