lui
lui

Reputation: 31

batch expand nested variable without delayed expansion

Is it possible to expand a nested variable without delayed expansion?

Example:

for /L %%i in (0,1,5) do ( echo.%somevar[%%i]% )

Here a more detailed example of what I'm trying to do (I cant paste my whole code here)

::%1 list of keys
::%2 output variable
SETLOCAL ENABLEDELAYEDEXPANSION
::parse some files and process information. Then it is stored into somevar
ENDLOCAL & (
    for /L %%i in (0,1,%somevar.count%) do (
        set "%2[%somevar[%%i].key%]=%somevar[%%i].value%"
    )
)

Upvotes: 3

Views: 352

Answers (1)

Compo
Compo

Reputation: 38579

Here's a few ideas for you, although I would recommend that you use delayed expansion, as in the third and fourth examples, and of those, advise that you use the fourth:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "somevar[0]=string zero"
Set "somevar[1]=string one"
Set "somevar[2]=string two"
Set "somevar[3]=string three"
Set "somevar[4]=string four"
Set "somevar[5]=string five"

Echo(
For /L %%G In (0,1,5) Do Call Echo(%%somevar[%%G]%%
Echo(
For /L %%G In (0,1,5) Do For /F Delims^=^ EOL^= %%H In ('Echo(%%somevar[%%G]%%') Do Echo(%%H
Echo(
For /L %%G In (0,1,5) Do %SystemRoot%\System32\cmd.exe /V /D /C Echo(!somevar[%%G]!
Echo(
For /L %%G In (0,1,5) Do SetLocal EnableDelayedExpansion & Echo(!somevar[%%G]!& EndLocal
Pause

Upvotes: 2

Related Questions