Check if process returns 0 with batch file
Asked Answered
H

6

43

I want to start a process with a batch file and if it returns nonzero, do something else. I need the correct syntax for that.

Something like this:

::x.bat

@set RetCode=My.exe
@if %retcode% is nonzero
   handleError.exe

As a bonus, you may consider answering the following questions, please :)

  • How to write a compound statement with if?
  • If the application My.exe fails to start because some DLL is missing will my if work? If not, how can I detect that My.exe failed to start?
Humpy answered 15/12, 2010 at 14:35 Comment(0)
L
69

ERRORLEVEL will contain the return code of the last command. Sadly you can only check >= for it.

Note specifically this line in the MSDN documentation for the If statement:

errorlevel Number

Specifies a true condition only if the previous program run by Cmd.exe returned an exit code equal to or greater than Number.

So to check for 0 you need to think outside the box:

IF ERRORLEVEL 1 GOTO errorHandling
REM no error here, errolevel == 0
:errorHandling

Or if you want to code error handling first:

IF NOT ERRORLEVEL 1 GOTO no_error
REM errorhandling, errorlevel >= 1
:no_error

Further information about BAT programming: http://www.ericphelps.com/batch/ Or more specific for Windows cmd: MSDN using batch files

Lassalle answered 15/12, 2010 at 14:40 Comment(5)
@Armen: That's why you need to check the return codes in reverse order. Start with the highest possible number and go down towards zero. Edit: I just realized your question is asking how to check if the return code is non-zero. In that case, ERRORLEVEL is exactly what you want. The statement Eduard posted will return TRUE as long as the return code is equal to or higher than the specified value.Electrotonus
You mean there is no way to check directly that some variable doesn't equal to some value?Humpy
if errorlevel 0 is always true, because it is true if errorlevel is 0 OR GREATER, read if /?Aroma
This doesn’t work on Windows 10.0.14393. When, e.g., a .net program exits due to an exception, it returns a negative error code. For ECHO %ERRORLEVEL% I get -532462766. and IF ERRORLEVEL 1 (ECHO failed) ELSE (ECHO succeeded) outputs succeeded. Thus this is not a reliable way to detect non-zero returns—it’s only a reliable way to detect greater than zero return values.Sitzmark
I suggest using IF %ERRORLEVEL% NEQ 0 or IF NOT ERRORLEVEL 1 IF ERRORLEVEL 0 «success action» instead to support detecting negative return values and depening on whether or not you know that %ERRORLEVEL% is safe to access.Sitzmark
L
17

How to write a compound statement with if?

You can write a compound statement in an if block using parenthesis. The first parenthesis must come on the line with the if and the second on a line by itself.

if %ERRORLEVEL% == 0 (
    echo ErrorLevel is zero
    echo A second statement
) else if %ERRORLEVEL% == 1 (
    echo ErrorLevel is one
    echo A second statement
) else (
   echo ErrorLevel is > 1
   echo A second statement
)
Lisa answered 15/12, 2010 at 15:6 Comment(6)
What language is this written in? The question is tagged "batch", so I'm pretty sure we're looking for a batch file. Your code won't work like you think it will.Electrotonus
I left out the %'s around my ERRORLEVEL's. Those have been add so now it will work correctly.Lisa
This answer seems to be WRONG as the if returns true if "error level is equal to or greater than Number". See answer by Eduard Wirch.Pornocracy
@Pornocracy - it works because it uses %ERRORLEVEL% and not ERRORLEVEL. With the %'s it's a normal batch variable, but without it it's a special form of if with the special greater than logic.Lisa
It is worth noting that this solution will work in most cases, but it will fail if there is a local batch variable named ERRORLEVEL. The local variable will take precedence in the evaluation and you will not get the desired value. To resolve that issue one would need to rely upon the syntax in the accepted answer.Debase
echo ErrorLevel is > 1 will write ErrorLevel is in a file named 1.Carnation
G
8

This is not exactly the answer to the question, but I end up here every time I want to find out how to get my batch file to exit with and error code when a process returns an nonzero code.

So here is the answer to that:

if %ERRORLEVEL% NEQ 0 exit %ERRORLEVEL%
Ga answered 24/1, 2019 at 21:55 Comment(4)
why the if? Just exit %errorlevel% is the same. Even just exit will implicit do exit %errorlevel%Equestrian
@stephan In my case I wanted to abort the rest of the script on error.Ga
@knocte: Using exit /B will stop execution of a batch file or subroutine and return control to the command processor or to the calling batch file or code immediately. If followed by an integer number the code will return an exit code or ERRORLEVEL equal to that number.Mymya
@knocte you are right, "exist" is my mistake ... ;-)Mymya
I
4

The project I'm working on, we do something like this. We use the errorlevel keyword so it kind of looks like:

call myExe.exe
if errorlevel 1 (
  goto build_fail
)

That seems to work for us. Note that you can put in multiple commands in the parens like an echo or whatever. Also note that build_fail is defined as:

:build_fail
echo ********** BUILD FAILURE **********
exit /b 1
Izettaizhevsk answered 15/12, 2010 at 14:43 Comment(5)
does your if check that return code is 1 or that it is nonzero?Humpy
You should better use if %errorlevel% NEQ 0, because the if errorlevel [number] is true if errorlevel is equal or greater than [number], try a look at if /?Aroma
@Aroma but that’s only if you know for sure nobody accidentally assigned to the ERRORLEVEL variable, right?Sitzmark
@Sitzmark IF ERRORLEVEL 1 is true for ERRORLEVEL >= 1 but false for negative ERRORLEVEL <= -1 whereas IF %ERRORELEVEL% NEQ 0 is true for both >= 1 and negative <= -1. Late comment, but maybe helpful to othersTranscribe
@Transcribe But checking %ERRORLEVEL% is not the same as checking ERRORLEVEL and could lead to unexpected behavior unless you are certain that all of the scripts you use follow this advice to never SET ERRORLEVEL=. So avoiding %ERRORLEVEL% is just a defensive technique.Sitzmark
S
1

To check whether a process/command returned 0 or not, use the operators && == 0 or not == 0 ||:

Just add operator to your script:

execute_command && (

       echo\Return 0, with no execution error
) || (
        echo\Return non 0, something went wrong
     )

command && echo\Return 0 || echo\Return non 0
St answered 15/9, 2020 at 9:4 Comment(0)
F
0

You can use below command to check if it returns 0 or 1 :

In below example, I am checking for the string in the one particular file which will give you 1 if that particular word "Error" is not present in the file and if present then 0

find /i "| ERROR1 |" C:\myfile.txt
echo %errorlevel%

if %errorlevel% equ 1 goto notfound
goto found
:notfound
exit 1
:found
echo we found the text.
Fanlight answered 4/6, 2020 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.