2

Believe me, I've searched around but I haven't had any joy with this seemingly innocuous problem.

NOTE: There are some posts and answers on SO surrounding issues with batch variables in for loops; it's likely that many will not realize that if blocks impose the same complications regarding expansion.

I'm far from a batch file aficionado, so I'm likely missing something basic, but when I run the following script:

set testVar1=Initial Value 1
echo testVar1 = %testVar1%
if 1==1 (
  echo In If Block
  echo testVar1 = %testVar1%
  set testVar1=In If Block
  echo testVar1 = %testVar1%
  set testVar2=Initial Value 2
  echo testVar2 = %testVar2%
)
echo Out of If Block
echo testVar1 = %testVar1%
echo testVar2 = %testVar2%

I get:

testVar1 = Initial Value 1
In If Block
testVar1 = Initial Value 1
testVar1 = Initial Value 1
testVar2 =
Out of If Block
testVar1 = In If Block
testVar2 = Initial Value 2

Whereas what I would expect to see is:

testVar1 = Initial Value 1
In If Block
testVar1 = Initial Value 1
testVar1 = In If Block
testVar2 = Initial Value 2
Out of If Block
testVar1 = In If Block
testVar2 = Initial Value 2

Within the if block, I change the value of testVar1 but when I echo it, it has the original value. Similarly, I create a new testVar2 variable within the if block but when I echo it, it has a null value.

Yet once I exit the if block, the values set within it are correct when echoed.

I've been very careful with spaces in declarations, as mentioned elsewhere, and I've tried several variations but the above is the most basic test I can devise to illustrate the issue.

As I said, no doubt I'm missing something obvious but I don't know what it is.

I've also tried adding setlocal EnableDelayedExpansion as I've seen mention of this, but it made no difference.

SteveCinq
  • 1,920
  • 1
  • 17
  • 22
  • 3
    using delayed expansion also requires to use `!var!` instead of a `%var%` in order to expand variable's values in brackets blocks. – npocmaka Jul 11 '18 at 21:49
  • 1
    Weird that you could not find an answer. All I did was put `enabledelayedexpansion` in the search box and it found a couple hundred answers. – Squashman Jul 12 '18 at 02:26
  • @Squashman Yeah, if you know what to look for, you will likely find it! Both suggested duplicates focus on `for` loops; I didn't expect a simple `if` block would have this kid of impact as there is no iteration, simply an enclosing block. I've edited my question to stress this. – SteveCinq Jul 12 '18 at 03:46
  • upvoted for your efforts to pin the problem down to the basics and a clear and understandable question. – Stephan Jul 12 '18 at 08:40
  • @Stephan Thank you, much appreciated! Answer is all credit to npocmaka. – SteveCinq Jul 12 '18 at 17:01

1 Answers1

1

Ok, yes, my ignorance. Here's the fix, thanks to npocmaka:

setlocal EnableDelayedExpansionset
testVar1=Initial Value 1
echo testVar1 = %testVar1%
if 1==1 (
  echo In If Block
  echo testVar1 = !testVar1!
  set testVar1=In If Block
  echo testVar1 = !testVar1!
  set testVar2=Initial Value 2
  echo testVar2 = !testVar2!
)
echo Out of If Block
echo testVar1 = %testVar1%
echo testVar2 = %testVar2%

(I hate batch files even more now!)

SteveCinq
  • 1,920
  • 1
  • 17
  • 22