41

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?

6 Answers 6

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

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.
    – Cody Gray
    Dec 15, 2010 at 14:46
  • You mean there is no way to check directly that some variable doesn't equal to some value? Dec 15, 2010 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, 2010 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, 2016 at 16:53
  • 2
    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.
    – binki
    Jul 25, 2016 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
)
6
  • 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
    Dec 15, 2010 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, 2010 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.
    – koppor
    Jan 7, 2016 at 10:12
  • 3
    @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 7, 2016 at 17:42
  • 1
    echo ErrorLevel is > 1 will write ErrorLevel is in a file named 1.
    – dan1st
    Jul 28, 2019 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%
4
  • why the if? Just exit %errorlevel% is the same. Even just exit will implicit do exit %errorlevel%
    – Stephan
    Jan 25, 2019 at 8:20
  • @stephan In my case I wanted to abort the rest of the script on error.
    – Techniquab
    Jan 30, 2019 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 4, 2022 at 22:04
  • @knocte you are right, "exist" is my mistake ... ;-)
    – udoline
    Jan 7 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
5
  • does your if check that return code is 1 or that it is nonzero? Dec 15, 2010 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, 2010 at 16:02
  • @jeb but that’s only if you know for sure nobody accidentally assigned to the ERRORLEVEL variable, right?
    – binki
    Jul 25, 2016 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, 2020 at 7:25
  • @Kristen 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.
    – binki
    Apr 24, 2020 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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.