20
...
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?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
TripShock
  • 4,081
  • 5
  • 30
  • 39

2 Answers2

35

in this case you need to escape the > like this

for /F %%F in ('dir /B %* 2^> nul') do (
RGuggisberg
  • 4,630
  • 2
  • 18
  • 27
  • 17
    Microsoft uses an intern to implement the command shell and we have to live with it for the rest of our lives... – bambams Jun 10 '15 at 17:14
  • 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. – Rolf Jan 19 '22 at 14:06
-6

I believe you need a delimiting space between the "2" and the ">". Without that delimiter my dir test output still displayed on the screen. Furthermore, I believe that by sending the output of the dir command to null will not provide any data back for the set to process.

Howard
  • 19
  • 1
  • 3
  • 3
    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. – Ansgar Wiechers Jun 14 '13 at 17:22