Suppose i have variable a with contents “123” and variable b123 with some text in it. For some reason i want to use variable a as part of second var name. Something like this:
SET a=123
SET b123=some_text_in_it
rem i want next line to output "some_text_in_it"
echo %b%a%%
so i want to concatenate text with var contents and use resulting string as variable name to echo that contents. Sample above doesn’t work and i see why, probably i need to add some kind of groupping. How to do it, prefferably in one line?
There are two common ways for this
CALLorDelayedExpansionThe
CALLvariant uses the fact, that a call will reparse the line a second time, first time only the%a%will be expanded and the double%%will be reduced to a single%call echo %b123%And in the second step the
%b123%will be expanded.But the
CALLtechnic is slow and not very safe, so the DelayedExpansion should be prefered.The DelayedExpansion works as the exclamation marks are expanded in a later parser phase than the expansion of the percents.
And that’s also the cause, why delayed expansion is much safer.
Edit: Method for arrays containing numbers
If you are operating with arrays which contains only numbers you could also use
set /ato access them.That’s much easier than the
FORorCALLtechnic and it works also in blocks.