Using Window’s FOR command to replace unix find -exec

Finding all directories within a directory recursively:

dir /a:d /s /b <dirname pattern>


dir /a:d /s /b tmpdir*

the /a:d flag restricts the results to directories, /s does a recursive search and /b prints the results in bare format.

Why bare format?  Because that makes it suitable as input to the FOR /F command.  Putting them together, you can delete all these directories with:

FOR /F “delims=” %i IN (‘dir /a:d /s /b tmpdir*’) DO rd /s /q “%i”

FOR’s /F allows, among other things, using the output of a command (in between the single quoted parentheses) as input to a command that’s repeated (the part after the DO).  In this case we’re executing RD (remove directory) on each directory that starts with the name tmpdir.

No comments:

Post a Comment