The first premise is that batch scripts will execute all commands in them, even if one fails.
@echo off
does_not_exist.exe
also_does_not_exist.exe
C:\Users\user>two_failures.bat
'does_not_exist.exe' is not recognized as an internal or external command,
operable program or batch file.
'also_does_not_exist.exe' is not recognized as an internal or external command,
operable program or batch file.
This necessitates the following error checking.
@echo off
does_not_exist.exe
if %errorlevel% neq 0 (
exit /b %errorlevel%
)
also_does_not_exist.exe
if %errorlevel% neq 0 (
exit /b %errorlevel%
)
C:\Users\user>error_check.bat
'does_not_exist.exe' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\user>echo %errorlevel%
9009
The second premise is command chaining. On Windows you use &&
to run a second command only if the first succeeds.
C:\Users\user>does_not_exist.exe && echo Second Command!
'does_not_exist.exe' is not recognized as an internal or external command,
operable program or batch file.
Command chaining does not short circuit when a batch script exits with a non-zero return code.
C:\Users\user>error_check.bat && echo Second Command!
'does_not_exist.exe' is not recognized as an internal or external command,
operable program or batch file.
Second Command!
Why is this the case?
COMMAND.COM
couldn't run more than once batch file at once; if you invoked one batch file from another,COMMAND.COM
would forget about the calling batch file. The workaround at the time was to spawn another copy ofCOMMAND.COM
to run the second batch file.CMD.EXE
operates in this mode by default for compatibility, but it also providesCALL
which can nest batch files (among other things). I assume you're tripping over the compatibility mode when you're trying to use batch files in a short-circuit without the use ofCALL
.