Batch: Array index returns blank

In this piece of code I'm trying to make a list fit the screen in WinPE by alternating into two columns. But when I put !modelsvar[%increment%]:~20! in the echo it returns only ~20. Strange because !modelsvar[%%D]:~20! works fine. I've tried many variations of ! and % but no luck. Anyone know if there's is a specific rule that I'm missing?

I have setlocal enabledelayedexpansion enabled

set modelsx=%counter%
set /a counter=0

for /l %%D in (1,2,%modelsx%) do (
set /a counter+=1
set /a increment=!counter!+1
ECHO !counter!. !modelsvar[%%D]:~20!      !increment!. !modelsvar[%increment%]:~20!
set /a counter+=1
)

Upvotes: 1

Views: 67

Answers (2)

aschipfl
aschipfl

Reputation: 34909

You're expanding increment immediately within ECHO, that is like %increment%, so the returned value is the one before the for loop executes.

Here is another work-around:

set modelsx=%counter%
set /a counter=0

for /l %%D in (1,2,%modelsx%) do (
    set /a counter+=1
    set /a increment=!counter!+1
    call ECHO !counter!. !modelsvar[%%D]:~20!      !increment!. %%modelsvar[!increment!]:~20%%
    set /a counter+=1
)

Upvotes: 1

Aacini
Aacini

Reputation: 67216

for /l %%D in (1,2,%modelsx%) do (
   set /a counter+=1
   set /a increment=counter+1
   for %%X in (!increment!) do (
      ECHO !counter!. !modelsvar[%%D]:~20!      !increment!. !modelsvar[%%X]:~20!
   )
   set /a counter+=1
)

Further details at Arrays, linked lists and other data structures in cmd.exe (batch) script

Upvotes: 1

Related Questions