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

查看:115
本文介绍了如何展开一个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 。即 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 - 这将使你的蝙蝠中的任何环境变量设置中使用你的的过程中,作为修改循环。

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.

设置AAA = BBB 设置BBB = CCC - 您的数据填充设置语句

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

FOR / F %%一个在('回声%AAA%')也呼应%% A ! - 告诉处理器循环,虽然只有一次,并采取了返回的第一个令牌从括号命令的运行(空间和制表应用默认的分隔符),并把它在var %%一个(批外,单个%都行)。如果你作为一个%%,您需要在不要块使用%%一个指定的变种。同样,如果你指定%%我,在你的不要块中使用%%我。请注意,让你的环境变量中的循环不要块内解决,需要围绕着它在的。 (你不需要在块,因为我最初发布 - 我曾在我的编辑更改)。

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

您的更新之间的区别,这就是你试图呼应环境变量在设置('回声%% %I%'),但没有的为一组变量的滞后扩展。假如你使用在('回声!%%我!'),你会看到你的 BBB CCC DDD 变量解决,但随后的不要块您的内环不会必须什么解决 - 你没有任何 111 环境变量。考虑到这一点,你可以简单地用下面的循环:

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