I'm new to batch scripting. Can you tell me what's the difference
between goto :eof
and :eof
? And why do some people write !ERRORLEVEL!
with exclamation marks?
Sorry for this noob questions but I couldn't find an answer yet. Thanks!
I'm new to batch scripting. Can you tell me what's the difference
between goto :eof
and :eof
? And why do some people write !ERRORLEVEL!
with exclamation marks?
Sorry for this noob questions but I couldn't find an answer yet. Thanks!
:eof
is a reference to the End Of File, a virtual label (does not need to be defined) where to jump to end the current batch context.
goto
is a command used to jump (interrupt the current flow and continue in another named point) to a label
goto :eof
is a jump to the end of the file
errorlevel
is the name of a variable that holds the exit code of the last executed command (if it sets it)
If you want to retrieve the value inside this variable you use %errorlevel%
.
But, as in batch files variables are replaced with its value at parse time (please, read here), sometimes it is necessary to indicate to the parser that this should not be done and delayed expansion syntax is used, so variable read operations are not replaced until the moment the command is executed. The syntax to indicate it to the parser is !errorlevel!
having delayed expansion enabled (setlocal enabledelayedexpansion
command).