1
C:\WINDOWS\system32>SetLocal EnableDelayedExpansion
C:\WINDOWS\system32>set/a Number1=3+9
12
C:\WINDOWS\system32>if !Number1!==9+3 (echo Good) else (echo Bad)
Bad
C:\WINDOWS\system32>if !Number1!==3+9 (echo Good) else (echo Bad)
Bad
C:\WINDOWS\system32>set/a i=9+3
12
C:\WINDOWS\system32>if !Number1!==%i% (echo Good) else (echo Bad)
Bad

I expected to see the last results (and maybe some others) to show Good as a result but did not! I think this is because it is a error with the SetLocal EnableDelayedExpansion but I need that in my code. So how do I get my expected result with SetLocal EnableDelayedExpansion. Thanks for any help provided =)

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Gerrard
  • 11
  • 1
  • 1
    Are you typing all those lines in command prompt?? in that case, `setlocal EnableDelayedExpansion` has no effect, and the `!!` expansion will therefore not work; you need to open a new `cmd` instance to enable delayed expansion: `cmd /V:ON` – aschipfl Oct 18 '15 at 14:33
  • In addition, you should use comparison operator `EQU` for numerical operations, because `==` forces *string* comparison... – aschipfl Oct 18 '15 at 14:39
  • Yes I typed all those in command prompt. And yes, after I tried it in a batch file, it worked! I'm also gonna try using EQU. Thanks :) :) :) – Gerrard Oct 18 '15 at 14:39

1 Answers1

2

setlocal EnableDelayedExpansion only works within batch files (see also setlocal /?), it has no effect when being typed into command prompt; therefore the !! expansion does not work.

To use delayed expansion in command prompt, you need to open a new cmd instance:

cmd /V:ON

You cannot do arithmetics in the comparison expressions of the if statement directly, you need to do all the calculations in advance.
In addition, you should use the comparison operator EQU for numeric operations, because == forces string comparison:

set /A Number1=3+9
set /A i=9+3
if !Number1! EQU %i% (echo Good) else (echo Bad)
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 1
    What do you mean with "`==` forces string comparison"? The numeric comparison have only sense with greater/less comparisons. If two values are equal/different, does not matter if they are taken as numbers or strings! – Aacini Oct 18 '15 at 15:37
  • I agree, `==` and `EQU` give the same result; I just wanted to point out that they do not behave exactly the same... – aschipfl Oct 18 '15 at 15:49
  • Excuse me. If `==` and `EQU` do not behave exactly the same, what is exactly the difference? Could you post any code that show this difference? – Aacini Oct 19 '15 at 01:29
  • @Aacini, [here](http://stackoverflow.com/q/31680385/5047996) is an example; and this one: `if 1 EQU 01` evaluates to True, where `if 1==01` is False... – aschipfl Oct 19 '15 at 07:44
  • 1
    Ok. Although this involves a different concept (because both values are _different based numbers_ that will never be generated by the same operations), I must admit you are right: `if 18 equ 0x12 echo True` – Aacini Oct 19 '15 at 14:07