?
Solved

batch file that checks the return code of executables it runs

Posted on 2011-04-20
13
Medium Priority
?
1,440 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.
0
Comment
Question by:mmingfeilam
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 16

Assisted Solution

by:jimbobmcgee
jimbobmcgee earned 332 total points
ID: 35436232
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
 
LVL 72

Accepted Solution

by:
Qlemo earned 336 total points
ID: 35436690
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
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 35436989
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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 35437007
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
 
LVL 72

Expert Comment

by:Qlemo
ID: 35437009
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 35437599
FWIW, exit /b is my preferred way to exit a batch script as well.

~bp
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 35437873
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 35437960
@jimbobmcgee

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

~bp
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 35443663
@jimbobmcgee

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

0
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 35444616
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
 
LVL 25

Assisted Solution

by:Ron Malmstead
Ron Malmstead earned 332 total points
ID: 35444676
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
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 35469922
@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
 
LVL 72

Expert Comment

by:Qlemo
ID: 35471022
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

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question