How do I return 0 for success ate the end of an MSDOS batch file?
Similarly, how do I return 1 (or other values) representing erroneous execution?
The most direct way is via
exit /b value,
However, in Windows 8.1 at least, that doesn't support
&& and
|| in the invoking command.
To make those operators work, exit via the end of the batch file, where you place a
cmd /c exit value, e.g.,
@echo off
setlocal
set E_FAIL=2147500037
set exit_code=%E_FAIL%
set /p a=Pretend to succeed (y/n)?
if "%a%"=="y" goto succeed
:fail
set exit_code=%E_FAIL% & goto finish
:succeed
set exit_code=0 & goto finish
:finish
cmd /c exit %exit_code%
For example, if that file is called
cmd_status.bat, then you can test it with
cmd_status && echo OK || echo Bah