We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

batch file that checks the return code of executables it runs

mmingfeilam
mmingfeilam asked
on
Medium Priority
1,497 Views
Last Modified: 2012-05-11
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.
Comment
Watch Question

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
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.
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
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.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
@jimbobmcgee

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

~bp

Commented:
@jimbobmcgee

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

Ron MalmsteadInformation Services Manager
CERTIFIED EXPERT

Commented:
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
Ron MalmsteadInformation Services Manager
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
@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.
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
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.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.