【问题标题】:Variables not set in the batch file [duplicate]批处理文件中未设置的变量[重复]
【发布时间】:2018-09-16 00:48:22
【问题描述】:
setlocal enabledelayedexpansion
If "%computername%"=="USER-PC" (
    set abc = ZZZ.bat
    echo %abc%
    pause
)

这里 abc 总是显示空白。可能的原因是什么?

【问题讨论】:

标签: batch-file delayedvariableexpansion


【解决方案1】:

您已经进行了一半,因为您启用了延迟扩展。但是,延迟扩展使用! 字符而不是%,所以您需要的是:

setlocal enabledelayedexpansion
if "%computername%"=="USER-PC" (
    set abc=ZZZ.bat
    echo !abc!
    pause
)

还要注意:

set abc = ZZZ.bat

不创建一个abc 变量,它创建一个abc<kbd>space</kbd> 并将其设置为<kbd>space</kbd>ZZZ.bat,按照:

C:\Users\pax> set abc = 1
C:\Users\pax> echo .%abc%.
.%abc%.
C:\Users\pax> echo .%abc %.
. 1.
C:\Users\pax> set xyz=1
C:\Users\pax> echo .%xyz%.
.1.

你会看到我已经删除了 = 字符周围的空格来解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2010-12-30
    • 2019-08-09
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多