1

I have a requirement to use nested variables for creating a folder based on a environment variables.

Assume I have variables listed below:

%ABC_ASIA_LOCATION% 
%ABC_EUROPE_LOCATION%
%ABC_US_LOCATION%

and I want to pass the country as variable like %ABC_%COUNTRY%_LOCATION%.

How do I achieve this in Windows utilizing batch scripting?

Nagendira
  • 201
  • 1
  • 3
  • 6
  • The information following this link is not directly related to nesting but can be helpful for variable value substitution [Variable Edit/Replace](https://ss64.com/nt/syntax-replace.html) if needed in further stages. –  Jan 27 '19 at 10:32

2 Answers2

1

you have to enclose each variable into %:

set "ABC=ABC"
set "COUNTRY=EUROPE
set "LOCATION=MUNICH
echo %ABC%_%COUNTRY%_%LOCATION%

Result: ABC_EUROPE_MUNICH

Or if you just want Country as a variable, keeping the rest fixed:

echo ABC_%COUNTRY%_LOCATION

Result: ABC_EUROPE_LOCATION

or if you want the whole thing to be a variable (a variable name containing another variable), you have to use delayed expansion:

setlocal enabledelayedexpansion
set country=EUROPE
set "ABC_EUROPE_LOCATION=a town in southern Germany"
echo !ABC_%country%_LOCATION!

which gives you: a town in southern Germany

Note: setlocal has no effect outside of batchfiles, so delayed expansion works only:
- in batchfiles
- when the command prompt was started with delayed expansion enabled (cmd /v:on) (by default, the command prompt runs with delayed expansion disabled)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 2
    Perhaps you should emphasize that `setlocal` _only_ works in batch files, and to use delayed expansion directly on the command line, one needs to issue `cmd /v` first. – Joseph Quinsey Jan 26 '19 at 20:28
  • 3
    @JosephQuinsey: yes, probably helpful for starters. (Such things are burned so deep into my brain that it's hard to remember, others may not know them...) I added a note. – Stephan Jan 27 '19 at 08:37
0

There are times when you need the nested variable to work inside a for loop, which already requires the !varname! syntax for the variable expansion. When this is the case, !ABC_%country%LOCATION!, will not work (reference Stephan's post on 9/6/2017 at 7:24). Neither will !ABC!country!_LOCATION!.

The following batch file demonstrates this. This is a somewhat contrived example to demonstrate the issue. In the subroutine, we can also set a variable to the nested value if you didn't want to do the work in the subroutine.

@echo off
setlocal enabledelayedexpansion
setlocal

set var1=value1
set var2=value2
set var3=value3
set var4=value4
set var5=value5

for %%A in (var1 var2 var3 var4 var5) do (
    set varname=%%A
    echo 1.This will not work: !varname!=!!varname!!
    echo 2.This will not work: !varname!=%!varname!%
    echo 3.This will not work: !varname!=!%varname%!
    echo 4.This will not work: %varname%=%varname%
    echo 5.This will not work: %varname%=%%varname%%
    call:NestedVar %%A
    
    call:getNestedVar new%%A %%A
    echo new%%A=!new%%A!

    echo.
    echo.

)

goto:eof
:NestedVar
    echo       This will work: %1=!%1!     (but only if setlocal enabledelayedexpansion is used)
    goto:eof

:getNestedVar
    REM Use: getNestedVar newVariableName varName
    REM Will set newVariableName to the value of varName
    echo Setting variable, %1=!%2!
    set %1=!%2!
    goto:eof