Solved

How do I capture the exit code of a program?

Posted on 2007-12-05
5
1,214 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

790 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