Link to home
Start Free TrialLog in
Avatar of Michael Lam
Michael Lam

asked on

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.
SOLUTION
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
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.
Avatar of Bill Prew
Bill Prew

FWIW, exit /b is my preferred way to exit a batch script as well.

~bp
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.
@jimbobmcgee

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

~bp
@jimbobmcgee

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

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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.
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.