computing
  • 1

Solved SETLOCAL EnableDelayedExpansion Script Not Working

  • 1

I am trying to write a command script with a FOR loop that sets a variable then compares it to a known value. Looking at examples, like the one below, I need to use EnableDelayedExpansion. Simple enough however I do not see it working. Here is a sample script I am running.

SetLocal EnableDelayedExpansion

set var=before
if “%var%” == “before” (
set var=after;
if !var! == “after” @echo if you see me, it worked
)

This gives me the following output –
C:\>test.cmd
C:\>SetLocal EnableDelayedExpansion
C:\>set var=before

C:\>if “before” == “before” (
set var=after;
if !var! == “after”
)

C:\>

What am I doing wrong here?

Share

1 Answer

  1. if "!var!"=="after;" @echo if you see me, it worked

    1) Watch your spaces. CMD is real picky about them. Most of the time you can get away with them, but not always.
    2) Missed a set of double quotes.
    3) “after”=/=”after;”

    How To Ask Questions The Smart Way

    • 0