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?

1 Answer 1

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
)
Sign up to request clarification or add additional context in comments.

2 Comments

It's not the problem here. I tried your solution but it put test instead of text
Yes, Alice, it is the problem and the solution. Maybe you didn't include the setlocal command as shown in jeb's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.