Delete specific folders from folder tree with DOS

12

JUN

09

Delete specific folders from folder tree with DOS

Lets say we want to delete all directories called "foo" with all their subdirectories in a folder structure. The DOS command would look like this:

for /f %D in ('dir /s/b/ad ^| find "foo"') do rd /s/q "%D"

The options of the dir command are as follows:

  • /s to display into all subdirectories as well
  • /b to show the bare format omitting time, date, type etc.
  • /ad to display only items with attribute (/a) directory (d).

The list is passed on to the find command using an escaped pipe (^|). Find will filter out anything but the folder name we are interessted in.

The for loop uses one parameter:

  • /f passes the first blank separated token from each line
which is the path to the directory and exactly what we need.

Now we delete the directory held in variable %D with the rd command and options:

  • /s to remove the entire directory with all files and subdirectories
  • /q to skip the confirmation that is prompted on deletions by default

Done. More information on the commands used you'll find here, here and here.

is the sum of one and six.