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?

2 Answers 2

36

in this case you need to escape the > like this

for /F %%F in ('dir /B %* 2^> nul') do (
Sign up to request clarification or add additional context in comments.

2 Comments

Microsoft uses an intern to implement the command shell and we have to live with it for the rest of our lives...
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.
-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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.