Variables not set in the batch file [duplicate]

不想你离开。 提交于 2019-12-02 02:01:19

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.

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