问题
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