Don't you hate it when you write a beautiful batch script, then run it only to discover it fails miserably? Isn't it even worse when it just flashes onscreen too quickly to discover what went wrong? But assuming things went fine, you don't want to "press any key to continue" every time, right?
Therefore, to pause on error in a batch script just check the error level and pause:
if NOT ["%errorlevel%"]==["0"] pause
Don't check for
1, even though it automatically checks for anything equal-to-or-greater (source), because technically scripts can return negative numbers. I also prefer "string" checking rather than
IF %errorlevel% NEQ 0 just for exactness.
And if you want your batch file to expose the last error, exit with the same code:
if NOT ["%errorlevel%"]==["0"] ( pause exit /b %errorlevel% )
And if you're feeling really fresh, repeat it with a reusable function:
-- do stuff -- CALL :CHECK_FAIL -- do more stuff -- CALL :CHECK_FAIL :: ======== FN ====== GOTO :EOF :: /// check if the app has failed :CHECK_FAIL [AT]echo off if NOT ["%errorlevel%"]==["0"] ( pause exit /b %errorlevel% )
(note the use of [AT] rather than @ due to coderwall auto-mention formatting)