Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
I am amazed there's no simple answer to this question. –  billpg Apr 5 '12 at 12:45
    
@billpg: simple as it could get: xcopy FROMDIR TODIR /s. See this SO answer: stackoverflow.com/a/14742810 –  eckes Mar 20 '13 at 7:42
1  
XCOPY deletes directories? –  billpg Mar 20 '13 at 11:24

6 Answers 6

up vote 44 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. :)

share|improve this answer
6  
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
1  
for /f %d in ('dir /s/b') do rmdir "%d" should work as rmdir cannot remove a non-empty folder –  seanyboy Sep 16 '09 at 13:14

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"
share|improve this answer
    
Everytime I run this command, my computer mysteriously reboots without warning nor BSOD. –  BlakBat Jun 14 '12 at 15:20

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

share|improve this answer
1  
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
1  
As user36580 wrote below, "it is likely you are running directly from the command line. In that case, change the double %% to a single %" –  nness Jul 15 '12 at 3:27

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 {} +
share|improve this answer

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

share|improve this answer

If you have Cygwin installed, you could do this:

find -type d -exec rmdir {} \;
share|improve this answer
3  
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

protected by nhinkle Jun 19 '11 at 22:58

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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