1

Is there a method to name an environment variable dynamically using another environment variable in a batch file?

Something like

numplayers=3
char%numplayer%atk=12 
echo char3atk  

with output

12
1

2 Answers 2

4

Given that you are unlikely to know beforehand the number assigned to %numplayers%, here are a few ways you can see the value of the variable:

Set "numplayers=3"
Set "char%numplayers%atk=12"
Call Echo %%char%numplayers%atk%%

 

Set "numplayers=3"
Set "char%numplayers%atk=12"
Set char%numplayers%atk

 

SetLocal EnableDelayedExpansion
Set "numplayers=3"
Set "char%numplayers%atk=12"
Echo !char%numplayers%atk!
Sign up to request clarification or add additional context in comments.

Comments

1

It's quite straightforward:

SET numplayers=3
SET char%numplayers%atk=12
ECHO %char3atk%

1 Comment

thank you I just realized i had a space in between the %% thank you for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.