Right now, I'm running through Steve Jansen's Batch tutorial here:
http://steve-jansen.github.io/guides/wi ... codes.html
I'm curious about when exactly I can use the && and || syntax, specifically to evaluate the success or failure of batch scripts I've writtten myself.
These seem to work just fine for exe's i.e. "core" Windows commands.
I have the following testing script called ErrorReturn.cmd
Code: Select all
@ECHO OFF
ECHO.
GOTO :MAIN
:MAIN
SETLOCAL EnableExtensions EnableDelayedExpansion
SET /A errno=0
CHOICE /C 01 /N /M "Select exiting code [0/1]:"
SET /A errno=%ERRORLEVEL%-1
ENDLOCAL & EXIT /B %errno%
I basically want to be able to choose 0 for success, 1 for failure at execution time, to play around with && and || syntax with follow-on commands.
When I run the script from the command line...
Code: Select all
ErrorReturn.cmd && ECHO Success
ErrorReturn.cmd || ECHO Failure
...it always seems to succeed with && regardless of what I enter at the CHOICE prompt. That is, the || syntax doesn't trigger a failure even when %ERRORLEVEL% is set to 1 by EXIT /B.
I did a little bit of research to try to figure this out, and there seems to be a distinction between the notion of an ERRORLEVEL, and the actual exit code of a program. Can someone explain to this to me in a bit more detail.
I do notice that when I do...
Code: Select all
%COMSPEC% /C ErrorReturn.cmd && ECHO Success
%COMSPEC% /C ErrorReturn.cmd || ECHO Failure
...I do get the behavior I'm looking for.
Not sure where the /B switch factors into things either? Or if it's just a matter of running a child CMD process? Any explanation of what's going on there would be helpful.