batch file that checks the return code of executables it runs

i want to write a DOS batch file that does the following in pseudocode:

1) runs executable 1,
2) depending on exe 1's exit code, either exit the batch file with a exit code that tells the scheduler that runs the bat file something went wrong, or execute the 2nd executable (exe 2 must not execute until exe 1 is done)
3) if exe 2's exit code is an error, exit the batch file with an error exit code.

thanks.
mmingfeilamAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The modern art of cmd batch programming allows to use a syntax of (using the same conventions as in the post above):
@echo off
exec01.exe || exit /b 1
exec02.exe || exit /b 22

Open in new window

exit /b is to be preferred over exit, because without /b cmd.exe is terminated instead of the (sub-)batch only.
The syntax of using && (for "success") and || (for "failure") allows more compact but still readable code.
0
 
jimbobmcgeeCommented:
The key term you are looking for here is %ERRORLEVEL%, e.g.
@ECHO OFF

REM run executable 1
.\exec01.exe

REM if exit code is not 0, exit the batch file with exit code 1...
IF NOT {%ERRORLEVEL%}=={0} EXIT 1

REM ...otherwise run executable 2
.\exec02.exe

REM check exit code of exec 2 and exit with code 2 if failed
IF NOT {%ERRORLEVEL%}=={0} EXIT 22

Open in new window


HTH

J.
0
 
jimbobmcgeeCommented:
Interesting, Qlemo; I knew about && but not ||.

I've tried exit /b before but have found it to be somewhat awkward when running from a Scheduled Task, so I tend to stick with exit. YMMV.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jimbobmcgeeCommented:
I'd add that both mine and Qlemo's approach assumes that an exist code of 0 is the only one that signifies success (as is usually the case).  I think the long-hand form might be the only option if you have different exit code parsing requirements.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I had my batch files exit and terminate my cmd.exe on too many occassions ... exit /b never had any side or "awkward" effects for me, and it is strongly recommended to be used in batch files.
0
 
Bill PrewCommented:
FWIW, exit /b is my preferred way to exit a batch script as well.

~bp
0
 
jimbobmcgeeCommented:
Well that's great that everyone has an opinion but, perhaps you'd run a test for me.  The OP talks about a scheduler.  It's not explicit but, for all intents and purposes, let's assume the bog-standard Task Scheduler that comes with Windows XP.

Go with the following dirt-simple batch file - c:\test.bat - and code it as follows:
@echo off
dir c:\
exit /b 5

Open in new window

Schedule this in Windows XP Task Scheduler and run it/allow it to run.

What is the Last Result, as reported in the Scheduled Tasks window?  For me, it is 0x0.  Every time.

Now, alter the code such that it uses exit 5.  Rerun the scheduled task.  Now what is your Last Result?  For me, it is 0x5.  Every time.  That's what I mean by awkward.

(I also note that, in Windows Server 2008 R2, the Last Run Result is 0x5 in both cases, so this is likely fixed in Vista, 7 and the like)

I agree that exit 5 closes the cmd.exe process when you run it from the command line; I've fallen foul of it myself, many times.  But at least exit 5 actually works inside all the schedulers I've tried.

So, in an effort not to completely tangent the question into a full-scale argument, I'd say:
if exit /b 5 works in your scheduler, use it
if it doesn't, try exit 5
J.
0
 
Bill PrewCommented:
@jimbobmcgee

Okay, fair enough, this does seem to be a problem in earlier versions of Windows, including XP. Thanks for clarifying.

~bp
0
 
ReneGeCommented:
@jimbobmcgee

In a normally working envirenment, do you mean, that for example, "Exit 5" would always work with XP and UP?

0
 
Ron MalmsteadInformation Services ManagerCommented:
Utilities make life easier....


Try this Utility ...

ifcmd.exe -v "value to compare" -o "operators" <optional -c "commandline if match"> <optional -i [/f][/t]Input>

Example...

cmd /c echo "TESTTHIS" | ifcmd -v "TESTTHIS" -o "=" -c "cmd.exe /c echo It the strings match"
Or,/
ifcmd -v "0" -o "!>" -c "cmd /c ECHO 0 is NOT greater than 0" -i /f"cmd.exe /c echo 0"
ifcmd.exe
0
 
Ron MalmsteadInformation Services ManagerCommented:
If the input is from another program... use   -i /f"program.exe + args"   ..otherwise you can PIPE the output into it.

The (-c "program.exe") will run in the current shell ...IF the input "matches" value to compare as the operators specify.

This can be chained to most programs that display output to evaluate them with another.
If you are "pipeing" input... and the input is another program...use the /f switch, otherwise it is treated as text.

ifcmd -v "TRUE" -i "TRUE"

Will return  1   ...for TRUE.

ifcmd -v "TRUE" -o "!=" -i "TRUE"

Will return 0  ...for FALSE...because (TRUE NOT EQUAL TO TRUE)...is FALSE.
0
 
jimbobmcgeeCommented:
@ReneGe

Yes, that's what I am saying.  In the test environments I have to hand, exit 5 works in 2000/XP/2003/2008R2, while exit /b 5 works in 2008R2.  From that, I'll project that exit /b 5 would work in Vista/7/2008 too.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Above example code tested with exit /b 5 on (all up-to-date with SPs):
   2000 Srv   0x1
   XP         0x0
   2003 Srv   0x5
   Vista      0x5

Open in new window

So it appears that starting with 2003  exit /b works with Task Scheduler.
It always works if used outside of Task Scheduler.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.