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.

This question already has an answer here:

How can I recursively delete empty folders?

"Empty" is considered to be true if the directory has no directory and no files exist except for useless auto-generated files (Thumbs.db, .DS_Store, etc.)

share|improve this question

marked as duplicate by Ƭᴇcʜιᴇ007, Excellll, Andrew Lambert, Kevin Panko, Tog Dec 19 '13 at 7:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
The 'useless files' criterion complicates the problem. If you know in advance that only unwanted files are in the directory tree, you can force a recursive delete. But only you can define what constitutes 'useless' so for this action to be generalised or automated, it would be safer to do it in two steps: first recursively delete all files with known unwanted names, then delete the directories (without force, in case there are wanted files in there). This becomes nontrivial and I'd use a script or Cygwin if I want that level of control. –  Andrew Crabb Feb 26 '13 at 18:32

6 Answers 6

for /f "delims=" %i in ('dir /ad /s /b') do @rd "%i"

  — bobbymcr

@bobbymcr 's answer works, however it would need to be run (n-1) times the maximum directory depth to be certain that there are no empty directories. This is because for will feed rd directories in the descending order dir lists them, attempting remove the parent directory before it has relieved it of its children.

Since rd will only remove directories that are already empty sort the children first and empty nests will be removed from a tree in a single pass.

Make a [meow].bat file, and run it from the directory you want to prune. The first paramiter is an alternative tempfile name to use if needed.

:: Asserts the default tempfile name if none is specified.
IF "%1"=="" (
set __tempfile=meow.tmp
) ELSE (
set __tempfile=%1
)
echo %__tempfile%

:: Checks to see that it won't destroy an existing file
IF NOT EXIST %__tempfile% (
:: Gets list of directories in descending depth.
dir /ad /b /s > %__tempfile%
:: Removes empty directories specified in the list sorted in reverse order
for /f "delims=" %%i in ('sort /R %__tempfile%') do @rd "%%i"
:: Deletes tempfile
del %__tempfile%
) ELSE (
:: if the tempfile already exists.
@ECHO Tempfile already exists, please delete %__tempfile% and try again.
)
share|improve this answer

Try this one: Remove Empty Directories - CNET Download.com

Empty directory folders are annoying, but not often a cause for great concern. Still, they build up over time, and Remove Empty Directories is a smart and fast solution. The tiny program greets you with a mostly blank pane and an icon key to understanding your results. There's a dialog box at the top to specify a particular folder or drive to search, although by default it checks your C drive.

share|improve this answer
    
Here is a tutorial about using Remove Empty Directories doztech.net/how-tos/… –  BullDozier1337 Aug 7 '10 at 18:57
    
Nice website Ryan. –  Moab Aug 8 '10 at 14:45

Use Command Prompt from Start/Run (or the combination WindowsR). Write cmd to start the console, then navigate to the drive the directories are on by inputting the drive letter (e.g. if they are on D:\ you put d:).

The path of the command line will change to the desired path. Then use the command cd to travel to the empty directories parent folder (cd D:\ParentDirectory\). You can use the Tab for Windows to put the entire path.

Once arrived at the parent directory use

del /F EmptyDirectory1

This method is the best one and also will delete folders or files that can't be deleted by Windows Explorer.

share|improve this answer
2  
with an answer as yawn worthy as that, i'm a bit reluctant to correct you.. If he had trouble opening a command prompt, he could ask a question on that. But this is really for "super users", so you can assume they can open a command prompt, and if they can't they can ask. Otherwise there's no end to it. MOVE THE MOUSE UP, MOVE THE MOUSE DOWN. Onto your answer, DEL won't delete directories. –  barlop Dec 27 '10 at 8:04

Here is me removing an empty directory:

C:\>md f  
C:\>rmdir f  
C:\>  

There, I created one, then removed it. If there are things in it then it is not empty.

You can use rmdir /s if there are things in it:

C:\>rmdir f  
The directory is not empty.  

C:\>rmdir /s f  
f, Are you sure (Y/N)? y  

C:\>

And you can do all that without the command prompt.

Recursion is a concept in programming. I don't think you mean that. And whether something is implemented using recursion or not, is irrelevant to you since no doubt you don't intend to limit yourself.. and you weren't just intellectually curious about implementations.

And if you are having trouble deleting a directory, then think for example, about what error you get when you try to delete the directory you are struggling to delete. You could always boot off a live CD and delete it.

share|improve this answer
    
Looks like Moab and probably velio figured out what you want to do. Scan through your hard drive searching for empty directories, or directories with little junk windows files, so you can delete them. I guess that's one way of saving space ;-) –  barlop Dec 27 '10 at 7:55
    
Actually on some operating systems term "recursively" is used for directory deletion. For example famous rm -rf. –  AndrejaKo Dec 27 '10 at 8:19
    
@AndrejaKo I know about rm -rf and recursive.. and I wrote with that in mind too, and I am pretty sure that in the rm case the implementation is recursive. –  barlop Jan 3 '11 at 19:36
    
I see velio and moab figured out what you want to do(it wasn't what I thought). Really you could have asked your question without redefining the word empty. It already has a meaning which is obvious and shared with english and even rmdir uses the term. Your question really needs a rewrite –  barlop Dec 4 '12 at 19:42

Here is a command line one-liner that will delete every actually empty (i.e. zero files) directory in the folder and below:

for /f "delims=" %i in ('dir /ad /s /b') do @rd "%i"

Basically this gets a recursive listing of all directories starting from the current and then attempts to remove each directory. The rd command will not remove a non-empty directory by default so your files should be safe.

share|improve this answer
    
Perfect thanks! –  mtone Feb 3 '13 at 6:06
    
I had to run this a few times. I think the recursion is top down. –  jozxyqk Mar 23 at 9:04

Belvedere can do that, but you'll need to be more specific than "useless" to be able to set up the right rules.

share|improve this answer

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