Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.