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.)
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.)
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.
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.
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.
)
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.
Belvedere can do that, but you'll need to be more specific than "useless" to be able to set up the right rules.
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.