The first premise is that batch scripts will execute all commands in them, even if one fails.
@echo offdoes_not_exist.exealso_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 offdoes_not_exist.exeif %errorlevel% neq 0 ( exit /b %errorlevel%)also_does_not_exist.exeif %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?