Jun 04 2009

How to Delete Empty Folders - FREE!

While performing a disk cleanup recently, I had cause to locate and delete any empty folders under a root folder. I knew there had to be a batch file command to accomplish this, but I couldn’t for the life of me remember what it was!

Enter Google.

five minutes of some keyword searching with surgical precision and piecing together commands yielded my solution.

DIR /AD/B/S | SORT > FOLDERLIST.BAT

How It Works.

The Dir command is the familiar directory list command that comes standard with all versions of Windows since 95. The magic is in the switches:

/A Displays files with specified attributes.
When applied to the “D” attribute, it returns directories
/B Uses bare format (no heading information or summary).
/S Displays files in specified directory and all subdirectories.

SORT is an often overlooked command for, you guessed it, sorting. Here, the results of the Dir command are piped (”|”) into the SORT command as input. The result of the SORT command is then redirected from the command prompt to a file called FOLDERLIST.BAT.

Kick it up a notch.

Now that you’ve created this list, say you want to automate the deletion of each entry in the list. This is where pumping the output to a “.bat” file comes in. Open the bat file in textpad, or notepad, or your text editor

For example:

Typing:
DIR /AD/B/S | SORT > FOLDERLIST.BAT

in my C:\Program Files\Microsoft Visual Studio 8\VC folder yields the following results:

empty-file-results

Next, open the file in a text editor, word, or open office and replace the beginning of each line with the RD command followed by a space and a single quote. Like so:

empty-file-replace

This should give you the following:

empty-file-first-replace

Notice how each line now begins with “RD “” This is the old DOS Remove Directory command. Don’t worry, it only removes empty directories. But you still need to add an ending quote to each line. This is where Word or Open Office is handy. You can do another search and replace, but this time search for “^p” (new paragraph) and replace with ““^p” (end quote and new paragraph).

Save the file, and you’re all done except the double clicking.

Blog Traffic Exchange Related Posts Blog Traffic Exchange Related Websites
  • Weight Loss Tips and Tricks When it comes to losing weight, you probably know the drill. Burn more calories than you consume, eat healthy, and be healthy and the results will happen. Most diets and weight loss plans do not have much substance which is why dieting in general is far from healthy for you.......
  • Skateboarding Tricks There are a wide variety of different skateboarding tricks that you can try to perform out there. They range significantly in terms of level of difficulty. There are a wide variety of different tricks that you can learn, depending on what your own skateboarding skills are and what types of......
  • Useless use of cat awards Here you will find some useless use of cat command in linux. Really interesting and quite informative. Useless Use of Cat Award If you've been reading comp.unix.shell or any of the related groups (comp.unix.questions inter alia) for any amount of time, this should be a familiar topic. I made this......
  • Working with Files Computer files are part of a framework for managing the data you create and store on your computer. When you create information in a program, such as a letter, you save that information in a file and assign the file a memorable name. When you want to work with the......
  • Pimp your ls Here are some of the things that you can do with your ls command. First off you can use alias's to help you type the most typical ls commands. I will give you some examples: alias ls ="ls --color --classify" alias ls.="ls -d .*" Very commonly used : alias......