批处理文件中未设置的变量 [英] Variables not set in the batch file

查看:77
本文介绍了批处理文件中未设置的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

此处abc始终显示为空白.可能是什么原因?

Here abc always shows blank. What could be the possible reason?

推荐答案

由于启用了延迟扩展,因此您位于中途.但是,延迟扩展使用!字符而不是%,因此您需要的是:

You're halfway there, inasmuch as you've enabled delayed expansion. However, delayed expansion uses the ! characters rather than %, so what you need is:

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

还请注意:

set abc = ZZZ.bat

不会创建一个abc变量,它会创建一个abcspace变量并将其设置为spaceZZZ.bat,如下所示:

does not create an abc variable, it creates an abcspace one and sets it to spaceZZZ.bat, as per:

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.

您会看到我已经删除了=字符周围的空格以解决此问题.

You'll see I've removed the spaces around the = character to fix this.

这篇关于批处理文件中未设置的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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