0

The following code:

@IF [1]==[1] (
  @SET test="This is a test"
  echo --%test%--
)

When run twice, outputs the following:

----
--"This is a test"--

Why does this first one not have any value in the variable?

Nashenas
  • 1,651
  • 1
  • 21
  • 25
  • 1
    Your problem is variable expansion. Maybe [this](http://stackoverflow.com/a/30177832/2861476) could help – MC ND Oct 15 '15 at 17:32
  • You're both right. I had trouble searching for this one, sorry for the duplicate. – Nashenas Oct 15 '15 at 17:36

1 Answers1

1

The () is actually a line continuation, so the entire if "line" is parsed (and variables substituted) before the set command every executes. You need to use enabledelayedexpansion:

@setlocal enabledelayedexpansion
@if [1]==[1] (
  @set test="This is a test"
  echo --!test!--
)
Brett Kail
  • 33,593
  • 2
  • 85
  • 90