I also can't find out how it should work.
I tried a different way (with finally 3 macros).
Syntax:
cmd /c "%~f0" label c1 c2 c3 RetVarName [VarName1 Value1 [VarNameN ValueN]]label - sub routine where the While loop is placed
c1 - variable name that should be compared
c2 - compare operator
c3 - value to compare with c1
RetVarName - variable name of the value that should be returned via errorlevel
VarName1 Value1 - variable name and value pairs for predefined variables used in the While loop
I'm stumbling upon a strange fault (see the REM line). Message: '"!_c2!" was unexpected at this time'
In this variable the compare operator will be saved, but not at the time when the macro variabe shall be assigned. In the next line I replaced
!_c2! with
lss and it works just fine.
It seems the cmd is parsing the syntax of IF even if it is not executed yet.
Is there a way to avoid that "early parsing" by any kind of escape sequence?Here's the code:
Code: Select all
@echo off &setlocal DisableDelayedExpansion
%$initWhile% :test
call :macros
:::::::::::::::::::::::::::::::::::::::::::::::::
cmd /c "%~fs0" test num lss 50 ret num 5 ret 6
echo %errorlevel%
pause
goto :eof
:test
%$While%
set /a num+=1
set /a ret+=num
echo !num! !ret!
%$Wend%
:macros
set LF=^
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"&rem TWO EMPTY LINES ABOVE REQUIRED!
set $initWhile=for /l %%I in (1 1 2) do if %%I==2 (%\n%
if defined _arg (%\n%
for /f %%J in ("!_arg!") do endlocal^&call %%J %%*%\n%
) else exit%\n%
) else setlocal EnableDelayedExpansion ^&set _arg=
set $While=(%\n%
setlocal EnableDelayedExpansion%\n%
call set "_args=%%*"%\n%
if "!_args!"=="" exit 1%\n%
for %%J in (!_args:* ^^=!) do if not defined _c1 (set "_c1=%%J") else (%\n%
if not defined _c2 (set "_c2=%%J") else (%\n%
if not defined _c3 (set "_c3=%%J") else (%\n%
if not defined _ret (set "_ret=%%J") else (%\n%
if not defined _var (set "_var=%%J") else (set "!_var!=%%J"^&set "_var=")%\n%
))))%\n%
for /l %%J in (0) do (%\n%
for /f "tokens=1,2" %%K in ("!_c1! !_ret!") do (%\n%
REM if not !%%K! !_c2! !_c3! exit !%%L!%\n%
if not !%%K! lss !_c3! exit !%%L!%\n%
)%\n%
set $Wend=))
goto :eof
Regards
aGerman