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

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

问题描述

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

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

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

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

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

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.

有什么想法可以从脚本中实现,也就是将变量AAA扩展为字符串CCC吗?

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

最新修改

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

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!

)

我想看

111
222
333

输出.

推荐答案

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

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!

剖析此答案:

setlocal enabledelayedexpansion-这将允许您在蝙蝠期间的任何环境变量设置在您的for循环过程中进行修改.

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=BBBset BBB=CCC-您的数据填充set语句

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

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

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).

您与更新后的示例非常接近.像这样尝试:

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!
)

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

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天全站免登陆