Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: DOS IF %ERRORLEVEL% construct  (Read 145573 times)

0 Members and 1 Guest are viewing this topic.

tale103108

  • Guest
DOS IF %ERRORLEVEL% construct
« on: September 02, 2009, 07:40:26 AM »

Ok, I need to test the successful execution of a program within a DOS batch file, print if program fails but continue if program succeeds.

Pseudo-code;

program.exe  # program that is executed and status to be checked

IF %ERRORLEVEL NEQ 0 ECHO "I failed" EXIT      # check status

otherwise continue with batch job
....

Need code example because DOS is driving me crazy ... should be simple but I am using

myprogram.exe

@IF %ERRORLEVEL% NEQ 1 GOTO ERROR
@IF %ERRORLEVEL% EQ 0 GOTO OK

:ERROR
ECHO "Program failed, please check this log file for errors ..."
GOTO END

:OK

mynestprogram.exe



:END

and it is not working

--------------------------------------------------------------------------------

billrich

  • Guest
Re: DOS IF %ERRORLEVEL% construct
« Reply #1 on: September 02, 2009, 08:26:18 AM »

C:\>type err.bat
Code: [Select]
@echo  off
rem  myprogram.exe  0
set errorlevel=%1
echo errorlevel = %errorlevel%
IF %errorlevel% EQU 1 GOTO ERROR
IF %errorlevel% EQU 0 GOTO OK

:ERROR
ECHO "Program failed, please check this log file for errors ..."
GOTO END

:OK

echo  mynestprogram.exe
:END

Output:

C:\>err.bat 0
errorlevel = 0
 mynestprogram.exe
C:\>err.bat 1
errorlevel = 1
"Program failed, please check this log file for errors ..."

C:\>

billrich

  • Guest
Re: DOS IF %ERRORLEVEL% construct
« Reply #2 on: September 02, 2009, 08:37:20 AM »

if /?

where compare-op may be one of:

    EQU - equal
    NEQ - not equal
    LSS - less than
    LEQ - less than or equal
    GTR - greater than
    GEQ - greater than or equal

Salmon Trout

  • Guest
Re: DOS IF %ERRORLEVEL% construct
« Reply #3 on: September 02, 2009, 09:00:08 AM »
Code: [Select]

IF %ERRORLEVEL% NEQ 0 (
    ECHO "I failed"
    EXIT     
    )


Or you can use GTR instead of NEQ (This is more usual)

billrich

  • Guest
Re: DOS IF %ERRORLEVEL% construct
« Reply #4 on: September 02, 2009, 09:29:06 AM »
Code: [Select]

IF %ERRORLEVEL% NEQ 0 (
    ECHO "I failed"
    EXIT     
    )


Or you can use GTR instead of NEQ (This is more usual)


Where is the test code and the output?  Did they go fishing?

billrich

  • Guest
Re: DOS IF %ERRORLEVEL% construct
« Reply #5 on: September 02, 2009, 09:34:43 AM »
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true


Quote
"Using batch files

With batch files, which are also called batch programs or scripts, you can simplify routine or repetitive tasks. A batch file is an unformatted text file that contains one or more commands and has a .bat or .cmd file name extension. When you type the file name at the command prompt, Cmd.exe runs the commands sequentially as they appear in the file.

You can include any command in a batch file. Certain commands, such as for, goto, and if, enable you to do conditional processing of the commands in the batch file. For example, the if command carries out a command based on the results of a condition. Other commands allow you to control input and output and call other batch files.

The standard error codes that most applications return are 0 if no error occurred and 1 (or higher value) if an error occurred. Please refer to your application help documentation to determine the meaning of specific error codes.

For more information about batch file operations, see the following topics:

• Using batch parameters
 
• Using filters
 
• Using command redirection operators"
 

Salmon Trout

  • Guest
Re: DOS IF %ERRORLEVEL% construct
« Reply #6 on: September 02, 2009, 09:35:25 AM »
Where is the test code and the output?  Did they go fishing?

 ::)

Not really necessary, but I'll humour you. Don't swim in my river, or you'll drown.


Code: [Select]
program.exe  # program that is executed and status to be checked
IF %ERRORLEVEL% NEQ 0 (
    ECHO "I failed"
    EXIT      
    )
otherwise continue with batch job

billrich

  • Guest
Re: DOS IF %ERRORLEVEL% construct
« Reply #7 on: September 02, 2009, 10:05:41 AM »
This is what Mr. Trout is fishing for:

EXIT
Quits the CMD.EXE program (command interpreter) or the current batch script.

EXIT  [ /B ]  [ exitCode ]

/B Specifies to exit the current batch script instead of CMD.EXE.
If executed from outside a batch script, it will quit CMD.EXE.
 
exitCode Specifies a numeric number.
If /B is specified, sets ERRORLEVEL that number.
If quitting CMD.EXE, sets the process exit code with that number.
 

Salmon Trout

  • Guest
Re: DOS IF %ERRORLEVEL% construct
« Reply #8 on: September 02, 2009, 10:15:40 AM »
This is what Mr. Trout is fishing for:


No it isn't. The OP clearly knows what the EXIT command does and also has the idea of checking errorlevel and asked how to display a message and then exit following a nonzero errorlevel.

One reason why this did not work may be because it should be EQU not EQ

Code: [Select]
IF %ERRORLEVEL% EQ 0 GOTO OK
Or, if you don't like parentheses you can use labels and gotos

Code: [Select]
myprogram.exe
if %errorlevel% neq 0 goto end
mynextprogram.exe

but this is more flexible

Code: [Select]
myprogram.exe
if %errorlevel% neq 0 (
    echo there was an error
    goto end
    )
mynextprogram.exe
:end

Or even these

Code: [Select]
myprogram.exe || goto end
mynextprogram.exe
:end

Code: [Select]
myprogram.exe && mynextprogram.exe





« Last Edit: September 02, 2009, 12:18:05 PM by Salmon Trout »

billrich

  • Guest
Re: DOS IF %ERRORLEVEL% construct
« Reply #9 on: September 02, 2009, 12:49:43 PM »
Thank you Mr. Trout.

You have answered all of tale103108's questions.

Too bad tale103108 does not provide any feedback.

Are you a Guru for batch files?


BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: DOS IF %ERRORLEVEL% construct
« Reply #10 on: September 02, 2009, 02:49:31 PM »
Thank you Mr. Trout.

You have answered all of tale103108's questions.

Too bad tale103108 does not provide any feedback.

Are you a Guru for batch files?




lol... it's amazing, I would have thought everyone would have figured out his secret by now...

Guess it's limited to a small subset, eh Salmon ;)
I was trying to dereference Null Pointers before it was cool.

Salmon Trout

  • Guest
Re: DOS IF %ERRORLEVEL% construct
« Reply #11 on: September 02, 2009, 03:34:18 PM »

lol... it's amazing, I would have thought everyone would have figured out his secret by now...

Guess it's limited to a small subset, eh Salmon ;)

Seems that way. You figured it out. I thought my ponderous prose style and choleric disposition would give me away to all, but it seems I have been lucky.