1.

Solve : Expand value of the variable name stored within another variable?

Answer»

Hello there,

I have the following variables defined in a script

SET PKG_NAME=CORE
set MY_%PKG_NAME%_HOME=c:\home


So this creates a variable named MY_CORE_HOME (but I dont know that from within the script).
Then if I try to acces my variable using MY_%PKG_NAME%_HOME:

> ECHO MY_%PKG_NAME%_HOME
MY_CORE_HOME

> echo %MY_%PKG_NAME%_HOME%
CORE

What should I do can to access the content of the MY_%PKG_NAME%_HOME and have for result c:\home ?

please help
thanks
You cannot do this. Batch variables do not work in that WAY. You cannot hide a variable INSIDE another.

Quote

@echo off
set PKG_NAME=CORE
set MY_%PKG_NAME%_HOME=c:\home
echo 1 MY_%PKG_NAME%_HOME
echo 2 %MY_%PKG_NAME%_HOME%
echo 3 %MY_CORE_HOME%
[/tt]

Quote
1 MY_CORE_HOME
2 PKG_NAME
3 c:\home
[/tt]
You need to invoke another level of nesting

call set MY_%PKG_NAME%_HOME=c:\home
call echo 1 MY_%PKG_NAME%_HOME

etc

what happens is that the %PKG_NAME% is evaluated and substituted into the expression

dont forget to double-up the %s inside a batch file
GrahamNice catch Graham


so finally to transfer the content of the variable into a one that i can access at the FIRST level I do:

>call set TEST=%MY_%PKG_NAME%_HOME%
>set TEST
TEST=c:\home

then I can use TEST directly for my manipulation!

Thanks

PS: I had to add % to get it work (see above)





Discussion

No Comment Found