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

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

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Lalatendu Bag
  • 11
  • 1
  • 3
  • 2
    Refer also to these posts: [Example of delayed expansion in batch file](https://stackoverflow.com/q/10558316), and [Declaring and using a variable in Windows batch file (.BAT)](https://stackoverflow.com/q/10552812) – aschipfl Apr 06 '18 at 09:34

1 Answers1

2

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953