5

I've this code :

if %Ret:~6,4% EQU %Year% (
SET test=text
ECHO %test%
) else (
ECHO NO
)

The code enters in the if loop but it returns always Echo is off! I've pay attention to the space before and after the =. Any ideas?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Alice
  • 313
  • 2
  • 5
  • 16

1 Answers1

7

Number #342 of this type of question this year.

Percent expansion occours when a block is parsed, before any line is executed.
So the echo %test% is expanded before the variable is set.

Therefor exists the delayed expansion, which expands when a line is executed.

setlocal EnableDelayedExpansion
if "%Ret:~6,4%" EQU "%Year%" (
  SET test=text
  ECHO !test!
) else (
  ECHO NO
)
jeb
  • 78,592
  • 17
  • 171
  • 225
  • 1
    It's not the problem here. I tried your solution but it put test instead of text – Alice Nov 15 '13 at 09:47
  • 1
    Yes, Alice, it is the problem and the solution. Maybe you didn't include the setlocal command as shown in jeb's answer. – foxidrive Nov 15 '13 at 10:30