Variables not set in the batch file [duplicate]

白昼怎懂夜的黑 提交于 2019-12-02 01:40:51

问题


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

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


回答1:


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
)

Note also that:

set abc = ZZZ.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.



来源:https://stackoverflow.com/questions/49685195/variables-not-set-in-the-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!