Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I capture the exit code of a program?

Posted on 2007-12-05
5
Medium Priority
?
1,243 Views
Last Modified: 2010-08-05
Hi,
How do I capture the exit code of a program from command prompt on windows?
If I ran test.exe, I want to know if test.exe returned 0 to see if it ran successfully.
Thanks
Jamie
0
Comment
Question by:jamie_lynn
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
Wod earned 800 total points
ID: 20416809
ceate a file named filename.bat and add this into it (without the START and END lines):
=======START=======
@echo off  
 
start /wait program.exe

 if errorlevel 0 goto 0

 if errorlevel 1 goto 1

 goto done

 :0  
 echo Script finished successfully
 goto done  

 :1
 echo ERROR: There was an error  
 goto done  
 
 :done

=======END=======

then execute the bat file from the command prompt
0
 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 800 total points
ID: 20416996
For this method to work you must test the highest error code first:

 if errorlevel 1 goto 1
 if errorlevel 0 goto 0

Another way to do this is to use the errorlevel environment variable:

 if %errorlevel%==0 goto 0
 if %errorlevel%==1 goto 1


0
 
LVL 7

Expert Comment

by:Wod
ID: 20417012
I don't think the order matters
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20417021
Try it. I did and I confirmed it.
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 400 total points
ID: 20423441
To explain though obviously SteveGTR is right here...

if you do  if errorlevel x it means if errorlevel is x or greater, this is because you may have wanted to do something like this

@echo off
myprogr.exe
if errorlevel 1 goto therewassomeerror
echo There was no error, continuing

but as SteveGTR says we now have the environment variable %errorlevel% which is much easier as you can check for specific value, greater then, less than etc. using normal IF commands etc.

Sorry not after points, just thought worth adding a "why".

Steve
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

972 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