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 (
Share:
12,088

Related videos on Youtube

TripShock
Author by

TripShock

Updated on September 15, 2022

Comments

  • TripShock
    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
    Ansgar Wiechers almost 11 years
    No. Changing 2> to 2 > 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
    bambams almost 9 years
    Microsoft uses an intern to implement the command shell and we have to live with it for the rest of our lives...
  • Rolf
    Rolf about 2 years
    Saved 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.