【问题标题】:Getting Variable name in CMD from other variables从其他变量获取 CMD 中的变量名称
【发布时间】:2014-03-28 07:37:15
【问题描述】:

所以我正在编写一个与 CURL 一起使用的简短 CMD 脚本,但遇到了一个我似乎无法通过 Google 解决的问题。

我想要的是根据其他变量的名称获取一个变量,例如:

    @echo off
    SET Example1 = Test1
    SET Example2 = Test2
    SET Example3 = Test3 (The variable data isn't this similar ofc)
    ...
    FOR %%G in (1,2,3) DO (
    ...
    *This is where I want to Echo out Something like Example%%G* 
    )

只是使用

    Echo Example%%G

不起作用,我似乎在网上找不到任何东西。

【问题讨论】:

    标签: batch-file cmd


    【解决方案1】:

    试试这个:

    @echo off
    setlocal enabledelayedexpansion
    SET Example1=Test1
    SET Example2=Test2
    SET Example3=Test3
    FOR %%G in (1,2,3) DO (
      echo. !Example%%G!
     )
    

    正如Magoo所指出的,SET命令中的变量和值之间不能有空格。

    【讨论】:

      【解决方案2】:

      批处理对SET 语句中的空格很敏感。 SET FLAG = N 将名为“FLAGSpace”的变量设置为“SpaceN”的值

      一旦你修复了它,输出它,除非你使用delayedexpansion(太多的例子......),尝试

      FOR %%x IN (1,2,3) DO CALL ECHO %%example%%x%%
      

      echo 可能是set "extra%%x=%%example%%x%%"

      除此之外,需要更多关于你实际在做什么的信息......

      【讨论】:

        猜你喜欢
        • 2015-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-03
        • 2019-11-30
        相关资源
        最近更新 更多