Check if process returns 0 with batch file
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, ho开发者_StackOverflow中文版w can I detect thatMy.exe
failed to start?
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
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
)
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%
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
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
- For details on Operators' behavior see: Conditional Execution || && ...
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.
精彩评论