Solved

How do I capture the exit code of a program?

Posted on 2007-12-05
5
1,223 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 200 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 200 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 100 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

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.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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