Redirect output of command in for loop of batch script
12,088
in this case you need to escape the > like this
for /F %%F in ('dir /B %* 2^> nul') do (
Related videos on Youtube
Author by
TripShock
Updated on September 15, 2022Comments
-
TripShock over 1 year
... for /F %%F in ('dir /B %* 2> nul') do ( ...
What I'm attempting to do here is discard the err output of the command (and loop over the stdout output). However, it complains:
2> was unexpected at this time.
Is this some way to achieve this?
-
Ansgar Wiechers almost 11 yearsNo. Changing
2>
to2 >
would redirect STDOUT instead of STDERR, thus preventing the processing of the actual directory listing. As @RGuggisberg correctly pointed out, the redirection operator must be escaped in the nested command. -
bambams almost 9 yearsMicrosoft uses an intern to implement the command shell and we have to live with it for the rest of our lives...
-
Rolf about 2 yearsSaved my day, Mr. Guggisberg :). I'd like to add: If you want to redirect stderr to stdout, by using 2>&1, you need to escape the & too.