Commonly you will run a sequence of commands and check each of them for successful completion. The usual way to do this is to check the errorlevel variable like so:
c:\mycommand.exe
if errorlevel 1 exit 1
(NOTE, you need to clear any environment variables called errorlevel because the Batch interpreter will return these instead of the built in variable if they are defined. Do this just like this: set errorlevel=)
Anyway, once you have checked each of your commands for errors, you will usually print out error messages and return your own error codes so that external programs calling your batch file will know what went wrong. You do this like so:
exit 2
Where 2 is an error code that means something specific to your script.
Testing these initially seems difficult because when you run the script it exits and you cannot check which error code was returned. There is a way to test these! Here it is:
cmd /k myscript.bat
echo %errorlevel%Hope that helps - send me questions/comments below!
No comments:
Post a Comment