The Computer Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Life in a Drop

--

Chapters
00:00 Is It Possible To Pipe A List Of Files To Rmdir On Windows?
00:31 Accepted Answer Score 6
01:12 Thank you

--

Full question
https://superuser.com/questions/276373/i...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#windows #commandline #batch #pipe #dir

#avk47



ACCEPTED ANSWER

Score 6


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.