There are two common ways for this CALL or DelayedExpansion
setlocal EnableDelayedExpansion SET a=123 SET b123=some_text_in_it rem i want next line to output "some_text_in_it" call echo %%b%a%%% echo !b%a%!
CALL uses the fact that the call will re-process the line a second time, the first time it will expand only %a% , and double %% will be reduced to one %
call echo %b123%
And in the second step, %b123% will be expanded.
But the CALL technique is slow and not very safe, so DelayedExpansion should be preferred.
DelayedExpansion works because exclamation points expand at a later phase of the parser than percent decomposition.
And that is also the reason why delayed expansion is much safer.
Edit: method for arrays containing numbers
If you work with arrays that contain only numbers, you can also use set /a to access them.
This is much simpler than the FOR or CALL technique, and it also works in blocks.
setlocal EnableDelayedExpansion set arr[1]=17 set arr[2]=35 set arr[3]=77 ( set idx=2 set /a val=arr[!idx!] echo !var! )
source share