42

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?
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434

6 Answers6

67

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

koppor
  • 19,079
  • 15
  • 119
  • 161
Eduard Wirch
  • 9,785
  • 9
  • 61
  • 73
  • @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. – Cody Gray - on strike Dec 15 '10 at 14:46
  • You mean there is no way to check directly that some variable doesn't equal to some value? – Armen Tsirunyan Dec 15 '10 at 14:47
  • 12
    if errorlevel 0 is always true, because it is true if errorlevel is 0 OR GREATER, read if /? – jeb Dec 15 '10 at 16:03
  • 2
    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. – binki Jul 25 '16 at 16:53
  • 2
    I suggest using [`IF %ERRORLEVEL% NEQ 0` or `IF NOT ERRORLEVEL 1 IF ERRORLEVEL 0 «success action»`](http://stackoverflow.com/a/10936093/429091) instead to support detecting negative return values and depening on whether or not you know that `%ERRORLEVEL%` is safe to access. – binki Jul 25 '16 at 17:19
18

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
)
shf301
  • 31,086
  • 2
  • 52
  • 86
  • 1
    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. – Cody Gray - on strike Dec 15 '10 at 15:13
  • I left out the %'s around my ERRORLEVEL's. Those have been add so now it will work correctly. – shf301 Dec 15 '10 at 15:46
  • 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](http://stackoverflow.com/a/4451060/873282). – koppor Jan 07 '16 at 10:12
  • 4
    @koppor - 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. – shf301 Jan 07 '16 at 17:42
  • 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. – Nathan Jul 11 '16 at 23:31
  • 2
    `echo ErrorLevel is > 1` will write `ErrorLevel is` in a file named `1`. – dan1st Jul 28 '19 at 13:26
6

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%
Techniquab
  • 843
  • 7
  • 22
  • why the `if`? Just `exit %errorlevel%` is the same. Even just `exit` will implicit do `exit %errorlevel%` – Stephan Jan 25 '19 at 08:20
  • @stephan In my case I wanted to abort the rest of the script on error. – Techniquab Jan 30 '19 at 14:02
  • @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. – udoline Nov 04 '22 at 22:04
  • @knocte you are right, "exist" is my mistake ... ;-) – udoline Jan 07 '23 at 14:15
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
Merky
  • 494
  • 2
  • 4
  • does your if check that return code is 1 or that it is nonzero? – Armen Tsirunyan Dec 15 '10 at 14:45
  • 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 /? – jeb Dec 15 '10 at 16:02
  • @jeb but that’s only if you know for sure nobody accidentally assigned to the `ERRORLEVEL` variable, right? – binki Jul 25 '16 at 17:20
  • @binki 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 others – Kristen Apr 24 '20 at 07:25
  • @Kristen But checking `%ERRORLEVEL%` [is not the same as checking `ERRORLEVEL`](https://devblogs.microsoft.com/oldnewthing/20080926-00/?p=20743) and could lead to unexpected behavior unless you are certain that all of the scripts you use follow [this advice to never `SET ERRORLEVEL=`](https://ss64.com/nt/errorlevel.html). So avoiding `%ERRORLEVEL%` is just a defensive technique. – binki Apr 24 '20 at 14:14
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
Io-oI
  • 2,514
  • 3
  • 22
  • 29
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.
Ajeet Malviya
  • 784
  • 1
  • 5
  • 9