This question already has an answer here:
-
Variables are not behaving as expected
1 answer
setlocal enabledelayedexpansion
If "%computername%"=="USER-PC" (
set abc = ZZZ.bat
echo %abc%
pause
)
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
)
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.