1

I have written below sample batch script, variable COUNT is getting updated but somehow MY_ROOT is not getting updated, am I missing something here ?

@echo off
setlocal ENABLEDELAYEDEXPANSION
set MY_ROOT= C:\
set COUNT=0

if 1 == 1 (  
  set MY_ROOT = D:\ 
  echo MY_ROOT = !MY_ROOT!

  set /A COUNT=10 
  echo Count = !COUNT!
)
:end

**o/p:**
MY_ROOT =  C:\
Count = 10

Thanks.

NDestiny
  • 983
  • 1
  • 9
  • 26
  • Possible duplicate of [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](http://stackoverflow.com/questions/26386697/why-is-no-string-output-with-echo-var-after-using-set-var-text-on-comman) – Mofi Jan 12 '17 at 07:08

2 Answers2

5

Spaces are significant on both sides of a string set statement. You are assigning a variable called "MY_ROOTSpace"

Magoo
  • 68,705
  • 7
  • 55
  • 76
2

One of the most confusing 'features' in batch files is that the spaces become part of the variable name.Remove the spaces around equal sign when you are using SET. You can use also quotes like in the code bellow for more safe value assignment.

@echo off
setlocal ENABLEDELAYEDEXPANSION
set "MY_ROOT=C:\"
set COUNT=0

if 1 == 1 (  
  set "MY_ROOT=D:\"
  echo MY_ROOT = !MY_ROOT!

  set /A COUNT=10 
  echo Count = !COUNT!
)
:end
npocmaka
  • 51,748
  • 17
  • 123
  • 166