windowsbatch-filedelayedvariableexpansion

Inner delayed expansion in a batch script (windows)


I need to make delayed expansion inside of delayed expansion, i.e. smth like:

!PARAMS[!BEFORE_LAST!]!

Of course, the above is not valid, so I tried to workaround it with for-loop, but with no success:

SETLOCAL EnableExtensions EnableDelayedExpansion

SET PARAM_COUNT=0
FOR %%P IN (%*) DO (
    SET /A PARAM_COUNT+=1
    SET PARAMS[!PARAM_COUNT!]=%%P

    IF !PARAM_COUNT! GTR 1 (
        SET /A BEFORE_LAST = !PARAM_COUNT!-1
        FOR /L %%G IN (!BEFORE_LAST!) DO SET BEFORE_LAST_PARAM=!PARAMS[%%G]!
        IF "!BEFORE_LAST_PARAM!"=="--buildroot" (
            REM perfrom some actions here
        )
    )
)
ENDLOCAL

How could I achieve the explained behavior?


Solution

  • delayed expansion inside delayed expansion doesn't work, try this:

    CALL SET "myvar=%%PARAMS[!BEFORE_LAST!]%%"