1

When I run the command:

if exist file.txt (echo yes) else (echo no) > "output.txt" 2>&1

in a command prompt, if 'file.txt' doesn't exist, a file 'output.txt' is created reading 'no', whereas if 'file.txt' does exist, 'yes' is output to the console window and the file 'output.txt' isn't even created. I also executed a batch file:

if exist file.txt (echo yes) else (echo no) > "output.txt" 2>&1
pause

and the result was the same, when 'file.txt' does exist 'yes' only appears in the console window. What's the reason, and how do I get all the output to redirect to 'output.txt' consistently?

1 Answer 1

3
(if exist file.txt (echo yes) else (echo no) )> "output.txt" 2>&1

I think the problem is only a matter of parenthesis

1
  • …which can be reduced to (If Exist "file.txt" (Echo yes) Else Echo no) 1>"output.txt", because there should be no stdout, and the Else command does not need to be parenthesized.
    – Compo
    May 30, 2021 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.