Redirect stdout and stderr from inside a batch file

前端 未结 2 1072
夕颜
夕颜 2020-12-30 01:36

Is there a way to redirect stdout and stderr for a batch file from inside it.

I\'m imagining something like

set STDOUT=stdout.log
echo Some text
a.ex         


        
2条回答
  •  长发绾君心
    2020-12-30 02:33

    Yes, you need to redirect and append stdout to your file (1>> %STDOUT%) and connect stderr to stdout (2>&1):

    set STDOUT=stdout.log
    echo Some text 1>> %STDOUT% 2>&1
    a.exe 1>> %STDOUT% 2>&1
    b.exe 1>> %STDOUT% 2>&1
    c.exe 1>> %STDOUT% 2>&1
    

    @EitanT correctly noted that your question doesn't necessarily imply writing both stderr and stdout into the same file. So for completeness, here's a version writing into separated files:

    set STDOUT=stdout.log
    set STDERR=stderr.log
    echo Some text 1>> %STDOUT% 2>> %STDERR%
    a.exe 1>> %STDOUT% 2>> %STDERR%
    b.exe 1>> %STDOUT% 2>> %STDERR%
    c.exe 1>> %STDOUT% 2>> %STDERR%
    

提交回复
热议问题