1

I am using the following code, and if I call the array.bat outside loop, you can the the value in output that is different, varying with the index that I used during call.

    @echo off

set empresas[0]=EPB
set empresas[1]=ENF
set empresas[2]=ESE
set empresas[3]=ESS
set empresas[4]=EBO
set empresas[5]=EMG
set empresas[6]=EMT
set empresas[7]=ETO

call array.bat len empresas length
call array.bat getitem empresas 0 empresa1
echo %empresa1%
call array.bat getitem empresas 1 empresa1
echo %empresa1%
echo %length%


for /l %%x in (0, 1, %length% ) do (

   call array.bat getitem empresas %x% empresa3

   echo %empresa3%
   echo %%x
)

That code generate the output:

   EPB
ENF
7
ETO
0
ETO
1
ETO
2
ETO
3
ETO
4
ETO
5
ETO
6
ETO
7

Only tha last value is being printted.

Roger Gusmao
  • 3,788
  • 1
  • 20
  • 17
  • 3
    Possible duplicate of [Windows Batch Variables Won't Set](https://stackoverflow.com/questions/9681863/windows-batch-variables-wont-set) – SomethingDark Nov 15 '17 at 17:46
  • It is not the same question. The value not change ONLY inside loop. – Roger Gusmao Nov 15 '17 at 17:58
  • 1
    It is _exactly_ the same question. Variables that get set inside of code blocks don't get updated until the end of the code block unless delayed expansion is enabled. – SomethingDark Nov 15 '17 at 18:32

1 Answers1

0

delayedexpansion trap again. Please search for delayed expansion on SO.

Try

call array.bat getitem empresas %%x empresa3

CALL echo %%empresa3%%

Note that %%x is the value of the metavariable %%x. %x% is the content of the environment variable x (which, according to your code, is undefined)

Reading about delayedexpansion will explain why the extra CALL and doubling of the % are required (one of many possible solutions)

Magoo
  • 77,302
  • 8
  • 62
  • 84