NDestiny
NDestiny

Reputation: 1171

Batch delayed expansion not working as expected

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.

Upvotes: 1

Views: 260

Answers (2)

Magoo
Magoo

Reputation: 79983

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

Upvotes: 5

npocmaka
npocmaka

Reputation: 57252

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

Upvotes: 2

Related Questions