Is it possible to pipe a list of files to RMDIR on Windows?

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.


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.