Delete Empty Folders.
If you ever have many folders and subfolders and some are empty it can become a real problem tidying it up. This is one solution that I have come up with, be warned that it is a little bit manual and there are probably easier solutions such as installing some clean-up software.
Change directory to where you want to clean up and run the following DOS command (or download this BAT file and run that).
DIR /AD/B/S | SORT /R > EMPTIES.BAT
The file EMPTIES.BAT now contains a list of all folders on your hard drive in reverse order. Use Word or another editor to put the filenames in quotes and add the prefix RD (with a space after RD) to every line in the file. In Word, you can do this easily by using Find and Replace to search for ^p (which represents the paragraph mark) and replace it with "^pRD " (quote,^, p, R, D, space quote), then hand-correct the first and last lines of the file if necessary. Save the modified EMPTIES.BAT file and exit your editor. Then simply launch the batch file. It will attempt the RD (remove directory) command on each folder, but the command will fail for any folder that is not empty.
How does it work? For
the DIR command, the switch /AD means select files whose attributes include
the Directory attribute (in other words, folders). The /B switch means give
a "bare" listing (just the filename) and /S means look in subfolders,
too (which, incidentally, modifies /B, so it shows the full pathname). The
output is piped (|) as input to the SORT command. The switch /R means sort
in reverse. Finally, the output of SORT is redirected (>) into the file
EMPTIES.BAT. Because we're sorting in reverse, every folder's subfolders precede
it in the list. If they are empty, then by the time the parent folder is processed,
it too will be empty.
![]() |
Last Updated Tuesday, October 26, 2004 |
|---|