Comments without increasing macro size
Posted: 13 Feb 2014 03:22
Hi,
I saw a post from Liviu ReturnVar macro revisitedwhere he used two versions of a macro, one commented and one uncommented.
Here I try to show a technic how to write macros with comments without increasing the macro size.
This uses some sort of the comment style invented by Ed.
Percent comments are a better choice here, as they are removed completly when the macro will be created.
But when you add line with only one comment, you get a problem with the multiline caracter at the line end.
Sample, this works and outputs "Word1Word2"
But with a comment it fails
It fails as the caret in the comment line is the first character in the line, after the parser has removed the percent comment.
But then the multiline caret from the first line will not only append the comment line, it will also escape the first character!
To avoid this you can simply use the obvious dummy redirect trick.
And here is a sample for a real macro, in this case the NUL redirection is executed only for the set-macro command and so it will not be a part of the macro itself.
jeb
I saw a post from Liviu ReturnVar macro revisitedwhere he used two versions of a macro, one commented and one uncommented.
Here I try to show a technic how to write macros with comments without increasing the macro size.
This uses some sort of the comment style invented by Ed.
Percent comments are a better choice here, as they are removed completly when the macro will be created.
But when you add line with only one comment, you get a problem with the multiline caracter at the line end.
Sample, this works and outputs "Word1Word2"
Code: Select all
echo Word1^
Word2
But with a comment it fails
Code: Select all
echo Word1^
%= REM this is a comment line%^
Word2
It fails as the caret in the comment line is the first character in the line, after the parser has removed the percent comment.
But then the multiline caret from the first line will not only append the comment line, it will also escape the first character!
To avoid this you can simply use the obvious dummy redirect trick.
Code: Select all
echo Word1<nul ^
%= REM this is a comment line%^
Word2
And here is a sample for a real macro, in this case the NUL redirection is executed only for the set-macro command and so it will not be a part of the macro itself.
Code: Select all
set LF=^
::Above 2 blank lines are required - do not remove
@set ^"\n=^^^%LF%%LF%^%LF%%LF%^<nul ^^"
rem ***
::: Timediff pTime1 pTime2 pResult
set $timediff=for /L %%n in (1 1 2) do if %%n==2 (%\n%
%= Read the parameters =%^
for /F "tokens=1,2,3 delims=, " %%1 in ("!argv!") do (%\n%
set "time1=!%%~1: =0!" %\n%
set "time2=!%%~2: =0!" %\n%
%= Convert the time format to milliseconds =%^
set /a "t1=((1!time1:~0,2!*60+1!time1:~3,2!)*60+1!time1:~6,2!-366100)*1000+1!time1:~-2!*10-1000" %\n%
set /a "t2=((1!time2:~0,2!*60+1!time2:~3,2!)*60+1!time2:~6,2!-366100)*1000+1!time2:~-2!*10-1000" %\n%
set /a "diff=t2-t1" %\n%
for /f "delims=" %%r in ("!diff!") do endlocal^& set "%%~3=%%~r" %\n%
) %\n%
) ELSE setlocal enableDelayedExpansion ^& set argv=,
jeb