Solved

How do I capture the exit code of a program?

Posted on 2007-12-05
5
1,189 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
Comment Utility
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
Comment Utility
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
Comment Utility
I don't think the order matters
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
Try it. I did and I confirmed it.
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 100 total points
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now