Solved

How do I capture the exit code of a program?

Posted on 2007-12-05
5
1,230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 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