Batch goto loses errorlevel

前端 未结 3 1046
失恋的感觉
失恋的感觉 2020-12-05 05:57

Consider the following bat, test.bat (PC01 is off):

mkdir \\\\PC01\\\\c$\\Test || goto :eof

If I run that bat from a command shell:

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 06:17

    To build a solution for batch files to set the return code while using goto :eof you can change your script a bit.

    mkdir \\\failure || goto :EXIT
    echo Only on Success
    exit /b
    
    :exit
    (call)
    

    Now you can use

    test.bat || echo Failed
    

    The only drawback here is the loss of the errorlevel.
    In this case the return code is set to false but the errorlevel is always set to 1, by the invalid (call)
    Currently I can't found any possible way to set both, the errorlevel and the return code to user defined values

提交回复
热议问题