Quantcast
Channel: Active questions tagged windows - Super User
Viewing all articles
Browse latest Browse all 9702

Why does Windows command prompt command chaining not short circuit when a batch file returns non-zero?

$
0
0

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?


Viewing all articles
Browse latest Browse all 9702

Trending Articles