I'm trying to understand where in the code exactly does GOTO :EOF
return to?
Here is the code:
SET count=1
FOR /f "tokens=*" %%G IN (somefile.txt) DO (call :subroutine "%%G")
GOTO :EOF
:subroutine
echo %count%:%1
set /a count+=1
GOTO :EOF
:EOF
is a predefined label as Microsoft explains in documentation for command GOTO. The help output by running in a command prompt windowgoto /?
explains also this special label for End Of File. But this predefined label is supported only with command extensions being enabled as by default.The help output by running in a command prompt window
call /?
and of course also the documentation for command CALL explain both thatgoto :EOF
should be used to exit a subroutine called withcall :Label
.A subroutine is nothing else than another batch file embedded within current batch file called with command
call
. If the subroutine is at end of the batch file, real end of file marks the end of the subroutine.But there can be multiple subroutines in a batch file.
So a command is needed for command interpreter to exit the subroutine on reaching a specific line in command processing and go back to the calling command line.
goto :EOF
as well asexit /B
can be both used everywhere to either exit a subroutine or exit the current batch file processing.In batch code in question the first
goto :EOF
is needed to exit batch file processing without an unwanted fall through to the subroutine code after finishing the loop.The second
goto :EOF
in batch code of questioner is for exiting the subroutine and continue processing in FOR loop in second line. It does not exit processing of the batch file, it exits only the processing of the subroutine.Note 1:
goto EOF
without a colon requires that there is really a line starting with:EOF
in the batch file, i.e. the jump labelEOF
must exist in the file.goto :EOF
always results in exiting subroutine/batch processing with command extensions enabled even if there is a labelEOF
in the batch file because of a line starting with:EOF
.Note 2: Command EXIT without parameter
/B
results always in exiting entire command process independent on calling hierarchy and independent on how the Windows command processor was started – with parameter/K
to keepcmd.exe
running as used when opening a command prompt window or with/C
to close after command processing finished as used on double clicking a batch file. Thereforeexit
without/B
should be used wisely in a batch file (best: never).Note 3:
exit /B
does not work with command extensions disabled as demonstrated by this code:Executing this batch file from within a command prompt window results in output of the error message:
In other words
exit /B
without an additional exit code is exactly likegoto :EOF
and depends therefore also on command extensions.exit
without/B
without or with an exit code works always.Note 4:
ERRORLEVEL
is not affected bygoto :EOF
, but the Microsoft GOTO documentation is mute on this topic.exit /B #
setsERRORLEVEL
to#
as documented by Microsoft.exit /B #
can be also used instead ofgoto :EOF
to exit a subroutine with a specific exit code evaluated on the command line calling the subroutine like on using the operators&&
or||
or on next command after calling command line withif errorlevel X
. However, explicitly exiting a batch file or a subroutine with a specific exit code is usually not needed as neithergoto :EOF
norexit /B
modify the current value ofERRORLEVEL
.For details on
ERRORLEVEL
behavior see::eof means "End of file". It's used to make the script finish without executing any of the commands below.
GOTO :EOF
is functionally equivalent toexit /B
, but both forms only works when Extensions are enabled. The test for this point is very simple:Compare previous code vs. this one:
This means that
GOTO :EOF
returns to the same point whereexit /B
would return.