如何将 CMD shell 变量扩展两次(递归) [英] How to expand a CMD shell variable twice (recursively)

查看:20
本文介绍了如何将 CMD shell 变量扩展两次(递归)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Windows XP CMD 命令行,我可以将变量扩展两次,如下所示:

set AAA=BBB设置 BBB=CCCfor/F "usebackq tokens=*" %i in (`echo %%AAA%%`) 做 echo %i

将回显CCC.IE.AAA 已经扩展为字符串BBB,然后变量BBB 已经扩展为CCC.

这在批处理脚本(即 .cmd 文件)中不起作用.将 %%AAA%% 更改为 %%%AAA%%%%%%%AAA%%%% 不会工作.

知道如何从脚本中实现这一点,即将变量 AAA 扩展为字符串 CCC?

后期编辑

发布的答案适用于我的简化示例,但非曲折的答案不适用于真实案例.这是一个扩展示例(不起作用),它说明了我实际尝试做的事情:

setlocal enabledelayedexpansion设置 LIST=BBB CCC DDD设置 BBB=111设置 CCC=222设置 DDD=333对于 (%LIST%) 中的 %%i 做 (for/F %%a in ('echo %%%i%') 做 echo !%%a!)

我想看

111222333

输出.

解决方案

从不那么曲折的解决方案的角度考虑,这也产生了您想要的 CCC.

setlocal enabledelayedexpansion设置 AAA=BBB设置 BBB=CCC对于 ('echo %AAA%') 中的/F %%a 做 echo !%%a!

剖析这个答案:

setlocal enabledelayedexpansion - 这将允许在您的 bat 期间使用任何环境变量设置,以便在您的 for 循环过程中进行修改.

set AAA=BBB, set BBB=CCC - 你的数据填充 set 语句

for/F %%a in ('echo %AAA%') do echo !%%a! - 这告诉处理器循环,尽管只有一次,并取出第一个令牌从括号中的命令运行返回(默认的空格分隔符和制表符应用)并将其放入 var %%a (在批处理之外,单个 % 即可).如果将该 var 指定为 %%a,则需要在 do 块中使用 %%a.同样,如果您指定 %%i,请在 do 块中使用 %%i.请注意,要在 for 循环的 do 块中解析您的环境变量,您需要将它括在 ! 中.(您不需要在 in 块中,正如我最初发布的那样 - 我已经在我的编辑中进行了更改).

您对更新的示例非常了解.像这样尝试:

@echo off设置本地启用延迟扩展设置 LIST=BBB CCC DDD设置 BBB=111设置 CCC=222设置 DDD=333对于 (%LIST%) 中的 %%i 执行 (for/F %%a in ('echo %%i') 做 echo !%%a!)

您的更新与此之间的区别在于您试图用 in ('echo %%%i%') 回显 in 集中的环境变量,但没有 ! 用于设置变量的延迟扩展.如果你在 ('echo !%%i!') 中使用 ,你会看到你的 BBBCCCDDD 变量已解析,但是您的内部循环的 do 块将无法解析 - 您没有任何 111 环境变量.考虑到这一点,您可以通过以下方式简化循环:

@echo off设置本地启用延迟扩展设置 LIST=BBB CCC DDD设置 BBB=111设置 CCC=222设置 DDD=333for %%i in (%LIST%) do (echo !%%i!)

Using the Windows XP CMD command-line I can expand a variable twice as follows:

set AAA=BBB
set BBB=CCC
for /F "usebackq tokens=*" %i in (`echo %%AAA%%`) do echo %i

will echo CCC. I.e. AAA has been expanded to the string BBB, and then the variable BBB has been expanded to CCC.

This doesn't work from inside a batch script (i.e. a .cmd file). Changing the %%AAA%% to either %%%AAA%%% or %%%%AAA%%%% doesn't work either.

Any idea how i can achieve this from within a script, namely to take expand the variable AAA to the string CCC?

Late Edit

The answers posted work for my reduced example however the non-tortuous answer doesn't work for the real case. Here's an extended example (which doesn't work), which illustrates what I was actually trying to do:

setlocal enabledelayedexpansion

set LIST=BBB CCC DDD
set BBB=111
set CCC=222
set DDD=333

for %%i in (%LIST%) do (

    for /F  %%a in ('echo %%%i%') do  echo !%%a!

)

I would like to see

111
222
333

output.

解决方案

Thinking in terms of a less tortuous solution, this, too, produces the CCC you desire.

setlocal enabledelayedexpansion
set AAA=BBB
set BBB=CCC
for /F  %%a in ('echo %AAA%') do  echo !%%a!

edit:

to dissect this answer:

setlocal enabledelayedexpansion - this will allow for any environment variable setting during your bat to be used as modified during the process of your for loop.

set AAA=BBB, set BBB=CCC - your data population set statements

for /F %%a in ('echo %AAA%') do echo !%%a! - This tells the processor to loop, albeit only once, and take out the first token that is returned (default delimiter of space and tab apply) from the running of the command in the parens and put it in the var %%a (outside of a batch, a single % will do). If you specify that var as %%a, you need to use %%a in your do block. Likewise, if you specify %%i, use %%i in your do block. Note that to get your environment variable to be resolved within the do block of the for loop, you need surround it in !'s. (you don't need to in the in block, as I originally posted - I have made that change in my edit).

edit:

You were very close with your updated example. Try it like this:

@echo off
setlocal enabledelayedexpansion
set LIST=BBB CCC DDD
set BBB=111
set CCC=222
set DDD=333

for %%i in (%LIST%) do (
    for /F %%a in ('echo %%i') do echo !%%a!
)

The difference between your update and this is that you were trying to echo the environment variable in the in set with in ('echo %%%i%'), but without the !'s for the delayed expansion of set variables. Were you to use in ('echo !%%i!'), you would see your BBB, CCC, and DDD variables resolved, but then the do block of your inner loop wouldnt have anything to resolve - you dont have any 111 environment variables. With that in mind, you could simplify your loop with the following:

@echo off
setlocal enabledelayedexpansion
set LIST=BBB CCC DDD
set BBB=111
set CCC=222
set DDD=333

for %%i in (%LIST%) do (echo !%%i!)

这篇关于如何将 CMD shell 变量扩展两次(递归)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆