Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file - Variables in Variables

Tags:

batch-file

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"?

like image 529
Jack Daniels Avatar asked Jul 09 '26 04:07

Jack Daniels


1 Answers

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%!
like image 130
Rafael Avatar answered Jul 11 '26 20:07

Rafael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!