Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a batch file for the Windows command prompt to delete all directories matching a certain expression.

I am first using DIR to return a plain list of directories matching the expression. I would like to pipe each line of output into the RMDIR command, like this:

DIR *.delete /A:D /B /S | RMDIR /S /Q

However the above command doesn't seem to work. I don't fully understand why this doesn't work and would be grateful to anyone who can offer an explanation.

share|improve this question
up vote 5 down vote accepted

You can use the following in your batch file:

FOR /f "tokens=*" %%a in ('dir *.delete /A:D /B /S') DO RMDIR /S /Q %%a

This uses the FOR command to loop through the output of a given command (in this case dir *.delete /A:D /B /S, and for each item returned it will run the command specified with the DO statement, RMDIR /S /Q. The item is referred to by the variable %%a.

The reason it doesn't work simply piping the DIR output into RMDIR is because you're sending the whole output (multiple lines) all at once as a single parameter to RMDIR. The FOR command breaks down this output, iterates through each item and then sends that to RMDIR one by one.

share|improve this answer
That worked perfectly. Thanks – user78755 Apr 28 '11 at 10:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .