How do I get the error level of commands in a pipe in Windows batch programming?

前端 未结 6 963
天涯浪人
天涯浪人 2021-01-03 18:22

Batch files return the error code of the last command by default.

Is it somehow possible to return the error code of a former command. Most notably, is it possible t

6条回答
  •  遥遥无期
    2021-01-03 18:44

    You can solve the problem by creating a wrapper around your command file:

    rem wrapper for command file, wrapper.cmd
    
    call foo.exe
    
    echo %errorlevel%
    
    if errorlevel 1 goto...
    

    Then append tee to the wrapper:

    wrapper.cmd | tee result.log
    

    Of course this does not exactly the same, e.g. if you want to log in several files in the wrapped file, it is not possible, but in my case it solved the problem.

提交回复
热议问题