Reputation: 31
I've used a for loop to split a string by spaces. I've also used a loop to make each word in its own variable such as var1=this, var2=is, var3=a, var4=test. That looks like "set var!count! = %%A"
That works. I just need to recall it. How do I do that? Logically, I think it would look like this: %var%count%%
Can someone explain to me how to get that? If I have the 'count' 1, what do I do to get "var1"?
Upvotes: 3
Views: 232
Reputation:
I don't see why you would need to do that. What you are looking for seems to be an array variable. You could solve the issue with:
set variable=this;is;a;test
for %%a in (%variable%) do (echo %%a)
For each value you want separate it with a ";" The output of this code will be:
this
is
a
test
Upvotes: 0
Reputation: 6630
When going through arrays in batch just use for /l
:
@echo off
setlocal enabledelayedexpansion
var0 = A
var1 = B
var2 = C
var3 = D
var4 = E
for /l %%a in (0, 1, 4) do (
Echo var%%a = !var%%a!
)
var0 = A
var1 = B
var2 = C
var3 = D
var4 = E
Upvotes: 0
Reputation: 3112
There is an easy way, you'll need to enable the delayed expansion, so first of all place setlocal enabledelayedexpansion
, then use exclamation marks to access these variables. Your script should look like this:
@echo off
setlocal enabledelayedexpansion
:: Here comes your loops to set the variables
echo/!var%count%!
Upvotes: 2
Reputation: 71
If you want to print out the contents of var1 outside the for statement use
Echo %var1%
To print out the contents of var1 inside the block first , delayed expansion needs to be enabled and instead of enclosing the variable references with percent signs , you use
Echo !var1!
Upvotes: -1