Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Expand value of the variable name stored within another variable  (Read 2774 times)

0 Members and 1 Guest are viewing this topic.

yperron

    Topic Starter


    Rookie
    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 
    yp

    contrex

    • Guest
    Re: Expand value of the variable name stored within another variable
    « Reply #1 on: October 23, 2007, 10:43:49 AM »
    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]

    gpl



      Apprentice
    • Thanked: 27
      Re: Expand value of the variable name stored within another variable
      « Reply #2 on: October 23, 2007, 04:39:08 PM »
      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
      Graham

      contrex

      • Guest
      Re: Expand value of the variable name stored within another variable
      « Reply #3 on: October 24, 2007, 12:00:06 AM »
      Nice catch Graham


      yperron

        Topic Starter


        Rookie
        Re: Expand value of the variable name stored within another variable
        « Reply #4 on: October 24, 2007, 09:31:58 AM »

        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 :o

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



        yp