June 20, 2012

External program execution handling in Windows .cmd files

It is common to use .cmd or .bat files to run some simple scheduled tasks. But if you run some external .exe and it reports an error, your script will continue running. So, how to stop execution and exit with error code (usually it is any exit code except 0)? Just write next ugly line after each external program execution:
if %errorlevel% neq 0 exit /b %errorlevel%
For example:
svn update
if %errorlevel% neq 0 exit /b %errorlevel%
sqlcmd -S . -i task.sql
if %errorlevel% neq 0 exit /b %errorlevel%

No comments:

Post a Comment