Windows DOS batch programming - error handling
Goal: execute multiple dos bat scripts, each script can call any number of other programs and every script must break execution if error is detected after each step. For safety reasons each script must return non zero value if error detected (to enable error detection if bat script is executed from other program as separate process).
To check if error occured after execution of DOS command or after call of external program use this (tested on Windows XP):
IF %ERRORLEVEL% NEQ 0 GOTO ERROR_HANDLER
where ERROR_HANDLER is label (jump point) in your bat script which contains error handling part of code.
Complete example: copy N files from location A to the B and after each step check for error, if error occured print 'Error' otherwise 'Sucess'. Note: 'exit 1' will cause command prompt to close, to avoid this, remove command 'exit 1':
@ECHO OFF
REM COPY all
copy /Y D:\exe\appserver\release\appserver.exe" D:\AppServerDeploy
IF %ERRORLEVEL% NEQ 0 GOTO ERROR_HANDLER
copy /Y "D:\exe\sokrates_spc\release\sokrates.exe" D:\SPCDeploy
IF %ERRORLEVEL% NEQ 0 GOTO ERROR_HANDLER
copy /Y "D:\tool\AdminTool\release\db_actualize.dll" D:\SPCDeploy
IF %ERRORLEVEL% NEQ 0 GOTO ERROR_HANDLER
echo.
echo Sucess!!!
GOTO QUIT
:ERROR_HANDLER
echo.
echo Error occured!!!
exit 1
:QUIT
echo.
References: