Aacini wrote: ↑22 Aug 2018 07:14
Just to complete this information, there are cases when a command ends with an ERRORLEVEL greater than 1 that does not means that the command failed because an error, but is just an indication of some condition.
Fair point. A good example is the ERRORLEVEL returned by HELP.
Requesting HELP for an invalid command gives an error message, but returns ERRORLEVEL 0.
Requesting HELP for a valid command gives the requested help, but returns ERRORLEVEL 1.
Code: Select all
@echo off
help bogusCommand
echo ERRORLEVEL = %errorlevel%
help rem
echo ERRORLEVEL = %errorlevel%
--OUTPUT--
Code: Select all
This command is not supported by the help utility. Try "bogusCommand /?".
ERRORLEVEL = 0
Records comments (remarks) in a batch file or CONFIG.SYS.
REM [comment]
ERRORLEVEL = 1
Aacini wrote: ↑22 Aug 2018 07:14
Also, although || detect a "return code" caused by an error, it does
not "set the ERRORLEVEL" by itself; this behavior depends on the type of command executed after the ||.
That can't be right. Surely ECHO is not setting the ERRORLEVEL in my examples. The subsequent command does not matter, as long as it does not itself set ERRORLEVEL. However, I do agree that a command executed by || can
mask the returned value of the previous command if the command after || itself sets ERRORLEVEL. I believe that is because || first sets the ERRORLEVEL based on the previous command's return code, and then the command after || may overwrite the ERRORLEVEL with its own value.
The value may not always be 0. For example:
Code: Select all
@echo off
(call )
REM
echo After REM: ERRORLEVEL = %ERRORLEVEL%
rd NotExists
echo After failed RD without ^|^|: ERRORLEVEL = %ERRORLEVEL%
rd NotExists || REM
echo After failed RD with ^|^|: ERRORLEVEL = %ERRORLEVEL%
--OUTPUT--
Code: Select all
After REM: ERRORLEVEL = 0
The system cannot find the file specified.
After failed RD without ||: ERRORLEVEL = 0
The system cannot find the file specified.
After failed RD with ||: ERRORLEVEL = 2
I claim that || always sets the ERRORLEVEL because of what happens when an invalid command is issued. This normally sets ERRORLEVEL to 9009, but if || is used, then the ERRORLEVEL becomes 1. I believe that the invalid command sets ERRORLEVEL to 9009, but has a return code of 1. The || then overwrites the 9009 ERRORLEVEL with the 1 return code. Note the use of CMD /C below,
based on an idea by jeb. CMD /C returns an error code, not ERRORLEVEL. This gives additional evidence that the return code of an invalid command is 1, even though the ERRORLEVEL is 9009.
Code: Select all
@echo off
(call )
bogusCommand
echo After invalid command without ^|^|: ERRORLEVEL = %ERRORLEVEL%
(call )
bogusCommand || REM
echo After invalid command with ^|^|: ERRORLEVEL = %ERRORLEVEL%
(call )
cmd /c "bogusCommand"
echo After bogusCommand executed by CMD /C: ERRORLEVEL = %ERRORLEVEL%
(call )
cmd /c "exit /b 9009"
echo Prove that CMD /C can return 9009: ERRORLEVEL = %ERRORLEVEL%
--OUTPUT--
Code: Select all
'bogusCommand' is not recognized as an internal or external command,
operable program or batch file.
After invalid command without ||: ERRORLEVEL = 9009
'bogusCommand' is not recognized as an internal or external command,
operable program or batch file.
After invalid command with ||: ERRORLEVEL = 1
'bogusCommand' is not recognized as an internal or external command,
operable program or batch file.
After bogusCommand executed by CMD /C: ERRORLEVEL = 1
Prove that CMD /C can return 9009: ERRORLEVEL = 9009
Dave Benham