This one:
Code: Select all
%echo /a% r={1+1}
Code: Select all
for %%n in (1 2) do if %%n==2 (%\n%
...
) else setlocal EnableDelayedExpansion ^& set argv= r={1+1}
Moderator: DosItHelp
Code: Select all
%echo /a% r={1+1}
Code: Select all
for %%n in (1 2) do if %%n==2 (%\n%
...
) else setlocal EnableDelayedExpansion ^& set argv= r={1+1}
AntonioAacini wrote: ↑12 Dec 2011 05:19
. . .
Suppose you have a macro-function that is usually executed this way:
How we may execute the macro in the following way?Code: Select all
for /F "tokens=1-26" %%a in ("param1 param2 resultvar") do %macroFunc%
Parameters placed after the macro needs to be taken and moved inside the FOR parentheses; this may be achieved by a two-step process:Code: Select all
%macroFuncWithParams% param1 param2 resultvar
1- Get the parameters
2- Execute the macro with them
That is, the macro must be executed two times: in the first step the parameters will be stored in an (argv) variable, and in the second step the macro will be really executed:However, the <rest of the line> is physically placed after the macro. This means that set argv= command must be the last part of the macro definition:Code: Select all
for %%s in (get_params exec_macro) do ( if %%s == get_params ( set argv=<rest of the line placed after the macro> ) else ( rem %%s == exec_macro for /F "tokens=1-26" %%a in (!argv!) do %macroFunc% ) )
There are still two parentheses after set argv=, but we can simply eliminate they because the FOR and ELSE part parentheses are placed here just for legibility:Code: Select all
for %%s in (1 2) do ( if %%s == 2 ( for /f "tokens=1-26" %%a in (!argv!) do %macroFunc% ) else ( set argv=<rest of the line...> ) )
This way, although the parts seems to be upside down, they execute in the right order...Code: Select all
for %%n in (1 2) do if %%n == 2 ( for /f "tokens=1-26" %%a in (!argv!) do %macroFunc% ) else set argv=