I have made some experiments.
Perhaps someone find it usefull, interesting or totally boring.
1. Rem have a different behavior than ::
Example1: Bracket test
Code: Select all
(
rem this works
)
(
:: This stops with a sytax error, I suppose that :: is only a lable that can't be called
)
2. The text after a rem or :: is not ignored
Example:
Code: Select all
rem one^
echo this line will NOT printed
:: one^
echo this line will NOT printed
rem one two^
echo this line is printed, because only the first token(after the rem) is evaluated for multilines
:: one two^
echo this line will NOT printed, because after a lable all tokens are valid
3. Expansion of %var% works at every position in both, rem and ::
Code: Select all
set "multi=^^"
rem %multi%
echo this line will NOT printed
:: %multi%
rem one two %~error%
set longString=5000 characters
rem %longString% %longString%
rem this stops with a syntax error, because the line is too long
But delayed expansion does not work in rem or ::
Code: Select all
rem !multi!
echo this line will be displayed
That's because the expansion phase of ! is after the multiline phase.
(Expansion phases will be explained in another post)
Example2:
2. !emptyCommand! can create a rem effect, so that the rest of the line will be ignored
Code: Select all
setlocal EnableDelayedExpansion
set "emptyCommand="
!emptyCommand! echo This line will be ignored
And now all as one file "remarks.bat"
Code: Select all
@echo off
setlocal DisableDelayedExpansion
cls
if "%1" NEQ "" goto :remoteExecuter
echo The rules of remarks
echo\
echo A rem ignores all after the first token, but wrong expressions can create an error like "rem one two %%~var%%
echo but the first token can contain a working multiline character
echo All tokens are expanded "%%variables%%", but only the first one can have an effect
echo A empty(remark) Token can be create with EnableDelayedExpansion and !EmptyCommand!
echo\
echo :: is not the same as rem
echo\
call :example remExample1
call :example remExample2
call :example remExample3
call :criticalExample remExample4
call :example remExample5
goto :eof
::###############################
:remExample1
rem IgnoreThis^
echo This line is also ignored, multiline with one token works
goto :eof
::End of function
::###############################
:remExample2
rem Ignore this^
echo This line is echo'd, the mutliline of rem more than one token does not work
goto :eof
::End of function
::###############################
:remExample3
set "remExp=Multiline^"
rem %remExp%
echo This line is also ignored, multiline with one token works, expansion phase is before rem
set "remExp=Multiline does not work^"
rem %remExp%
echo This line is echod, the mutliline of rem more than one token does not work
goto :eof
::End of function
::###############################
:remExample4
rem show that expansion of % in a line is before the rem ignores the rest of the line
rem The expansion results in a very long line with 10000 characters, that results in a syntax error, the batch stops immediatly
setlocal EnableDelayedExpansion
set "longStr=."
for /L %%c in (1,1,13) DO set longStr=!longStr:~0,2500!!longStr:~0,2500!
rem Now we heave a string with 5000 characters, and build a rem line with 2 times 5000 = 10000 characters
rem The next line crashes the batch file
rem tokenOne %longStr% %longStr%
(
ENDLOCAL
goto :eof
)
::End of function
::###############################
:remExample5
echo show that expansion of !emptyCommand! can create a rem effect, so that the rest of the line will be ignored
setlocal EnableDelayedExpansion
set "emptyCommand="
!emptyCommand! echo This line will be ignored
(
ENDLOCAL
goto :eof
)
::End of function
::################ TODO
:TODO
echo\
echo Rem test with Delayed expansion
set "sig=one two"
set "vType=type x"
set "rem=rem one two^"
set "x=&"
set REM
echo\--
setlocal EnableDelayedExpansion
echo REM is !REM! -
echo vType is !vType! -
goto :eof
goto :eof
############ END OF TODO
::End of function
::###############################
:example
call :showCode %~1
echo\--------------- begin of output of function :%~1
setlocal
call :%~1
endlocal
echo\--------------- end of output
echo\
echo Press any key
pause > nul
goto :eof
::End of function
::###############################
:executeRemote
start /min /wait cmd /c %~f0 %*
set remoteErrorlevel=%errorlevel%
goto :eof
::End of function
::###############################
:remoteExecuter
call %* 1> remoteExecute.log 2>&1
goto :eof
::End of function
::###############################
:criticalExample
setlocal
call :showCode %~1
call :executeRemote :%*
echo\--------------- begin of output of function :%~1
type remoteExecute.log
del remoteExecute.log 2> nul
echo\--------------- end of output, with ERRORLEVEL=%remoteErrorlevel%
echo\
echo Press any key
pause > nul
(
endlocal
goto :eof
)
::End of function
::###############################
:showCode
SETLOCAL DisableDelayedExpansion
rem Disable it to be able to echo "!" and "^"
echo\
echo ########## Example %~1 ################# Code of the example
set "found="
echo ---------------------
FOR /F "tokens=*" %%L IN (%~f0) DO (
if "%%L" == ":%~1" set found=1
rem setlocal EnableDelayedExpansion
if defined found (
rem set "var=%%L"
rem echo\ !var:^^=#!
echo %%L
if "%%L" == "::End of function" goto :showCode.break
)
)
:showCode.break
echo ---------------------
echo\
(
endlocal
goto :eof
)
::End of function
hope it is not too boring
jeb