In a
for /f
loop, a command that can be executed can be put within single quotes. In such a case, the
for /f
loop iterates over the lines produced by the quoted command.
Filtering the output of a command
It might be more useful if the output is filtered with
find
and then the fourth
token (that is the directory name) is echoed.
Note that special characters such as the pipe or ampersand need to be escaped with the caret:
@for /f "tokens=4" %%a in ('dir ^| find "<DIR>"') do @echo %%a
This example, of course, is for demonstration purposes only, the canonical way to display directories is
dir /od