This is a really strange and interessting bug!
The cause will be obvious when I used CALL
instead of GOTO
.
goto :notExist || call :someLabel
You get an error message like
Illegal to call a label outside of a batch file.
Obviously the parser switches to a cmd-line context here!
This is done when the first goto fails to find the label.
When you use first a call
all works fine.
call :noLabel 2>nul || goto :thisWorks
This seems to be a general side effect of a failing goto
.
When a goto fails, it normally stops immediatly the batch file.
But with the ||
operator the next command will be forced to execute.
But it seems that it works more like an exit /b
, so you can use this effect to leave a function.
@echo off
setlocal DisableDelayedExpansion
set var=111
call :myFunc
set var
exit /b
:myFunc
setlocal EnableDelayedExpansion
set var=222
goto :noLabel 2>nul || set var=333!var!
echo This will be never reached
exit /b
The interessting output
var=333!var!
So the goto :noLabel
acts like an exit /b
and it also done the implicit ENDLOCAL
before the part || set var=333!var!
is executed.
The same issue (but without the ||
operator) was discussed at Dostips: Rules for label names vs GOTO and CALL