双延迟批量扩张? [英] Double delayed expansion in batch?

查看:177
本文介绍了双延迟批量扩张?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想如果让任何意义,我加倍延迟扩展。这里是我想要的。

I am trying to double my delayed expansion if that makes any sense. Here is what I want.

set var1=hello
set var2=var1
set var3=var2
echo %!%var3%!%

再有你好显示。这不是我的实际code,但我怎么需要它来工作的例子。

and then have hello be displayed. This is not my actual code but an example of how I need it to work.

推荐答案

您可以添加一个呼叫和环绕像这种双重百分比您的第一延迟扩展。

You could add a call and surround your first delayed expansion with double percents like this.

@echo off
setlocal enabledelayedexpansion

set "var1=hello"
set "var2=var1"
set "var3=var2"

call echo %%!%var3%!%%

似乎可怕费解给我,虽然。我可能会重写剧本,使不需要这种挂羊头卖狗肉,如果我是我。

Seems horribly convoluted to me, though. I'd probably rewrite the script to make such trickery not needed if I were me.

编辑:由于这里有被添加这么多的解决方案,我将提出一个又一个。下面是将遵循变量的线从开始到结束,即使它的1000层深的子程序。只是作为一个学术活动。

Since there are so many solutions being added here, I'll propose another one. Here's a subroutine that will follow the line of variables from beginning to end, even if it's 1000 levels deep. Just as an academic exercise.

@echo off
setlocal

set "var1=hello"
for /L %%I in (2,1,1000) do (
    set /a X = %%I - 1
    setlocal enabledelayedexpansion
    for %%x in (!X!) do endlocal & set "var%%I=var%%x"
)

call :follow var1000
goto :EOF

:follow <varname>
setlocal enabledelayedexpansion
set "var=!%~1!"
:follow_loop
if defined !%var%! (
    set "var=!%var%!" & goto follow_loop
) else (
    echo !%var%!
)

和这里的另一个使用批处理+ JScript的混合动力(因为JScript的,而环比一批快转到循环)。

And here's another using a batch + JScript hybrid (because the JScript while loop is faster than a batch goto loop).

@if (@CodeSection == @Batch) @then

@echo off
setlocal

set "var1=hello"
for /L %%I in (2,1,1000) do (
    set /a X = %%I - 1
    setlocal enabledelayedexpansion
    for %%x in (!X!) do endlocal & set "var%%I=var%%x"
)

cscript /nologo /e:JScript "%~f0" "var1000"
goto :EOF

@end // end batch / begin JScript hybrid chimera
var env = WSH.CreateObject('Wscript.Shell').Environment('Process'),
    itm = WSH.Arguments(0);

while (env(itm)) itm = env(itm);

WSH.Echo(itm);

您搬家,Aacini。

Your move, Aacini.

这篇关于双延迟批量扩张?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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