0

我是批处理脚本的新手,我正在编写一个批处理脚本来设置环境变量。下面是我使用的批处理脚本。

if 1 == 1 (
    setlocal enableextensions enabledelayedexpansion
    SET name1=%1_hello
    endlocal & SET name=%name1%
    echo varaiable %name%
    goto :eof
)

但我面临的问题是变量name没有被设置并且name1inendlocal & SET name=%name1%总是为空或从以前设置的环境变量中获取它。但是相同的代码没有if statement工作。

    setlocal enableextensions enabledelayedexpansion
    SET name1=%1_hello
    endlocal & SET name=%name1%
    echo varaiable %name%
    goto :eof

在上面的代码中,name1变量被设置并被name显示。有人可以帮我摆脱这个问题。为什么endlocal / setif 语句的行为不同?

4

1 回答 1

3

当 IF 语句被解析时——从IF到它的右括号, ALL%var%被这些变量的内容替换,因为它们在该行被解析时,即在执行之前。

于 2013-03-19T12:47:06.283 回答