【问题标题】:Why is SET inside of an IF ignored on the first run? [duplicate]为什么在第一次运行时忽略 IF 内部的 SET? [复制]
【发布时间】:2015-10-15 17:29:37
【问题描述】:

以下代码:

@IF [1]==[1] (
  @SET test="This is a test"
  echo --%test%--
)

运行两次时,输出如下:

----
--"This is a test"--

为什么第一个在变量中没有任何值?

【问题讨论】:

  • 你的问题是变量扩展。也许this 可以提供帮助
  • 你说得对。我在搜索这个时遇到了麻烦,抱歉重复了。

标签: batch-file


【解决方案1】:

() 实际上是一个续行,因此在每次执行 set 命令之前解析整个if“行”(并替换变量)。你需要使用enabledelayedexpansion:

@setlocal enabledelayedexpansion
@if [1]==[1] (
  @set test="This is a test"
  echo --!test!--
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 2021-06-17
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 2021-01-12
    相关资源
    最近更新 更多