Windows批处理命令嵌套变量 [英] windows batch command nested variable
问题描述
我需要使用嵌套变量基于环境变量创建文件夹.
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%
,我想将国家作为变量传递给%ABC_%COUNTRY%_LOCATION%
.
and I want to pass the country as variable like %ABC_%COUNTRY%_LOCATION%
.
如何在Windows中使用批处理脚本实现此目的?
How do I achieve this in Windows utilizing batch scripting?
推荐答案
您必须将每个变量包含在%
中:
you have to enclose each variable into %
:
set "ABC=ABC"
set "COUNTRY=EUROPE
set "LOCATION=MUNICH
echo %ABC%_%COUNTRY%_%LOCATION%
结果:ABC_EUROPE_MUNICH
或者,如果您只想将Country
作为变量,请保持其余部分不变:
Or if you just want Country
as a variable, keeping the rest fixed:
echo ABC_%COUNTRY%_LOCATION
结果: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!
为您提供:a town in southern Germany
注意:setlocal
在批处理文件之外没有任何作用,因此延迟扩展仅适用于:
-在批处理文件中
-当命令提示符在启用延迟扩展(cmd /v:on
)的情况下启动时(默认情况下,命令提示符在延迟扩展 dis abled下运行)
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)
这篇关于Windows批处理命令嵌套变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!