Solved SETLOCAL EnableDelayedExpansion script not working

February 6, 2012 at 12:52:36
Specs: Windows 7
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?


See More: SETLOCAL EnableDelayedExpansion script not working

Report •


#1
February 6, 2012 at 13:42:52
✔ Best Answer
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


Report •

#2
February 6, 2012 at 13:48:00
@echo off
setlocal EnableDelayedExpansion

set var=before
if "%var%"=="before" (
  set var=after
  if "!var!"=="after" echo.if you see me, it worked
)

When embracing variables/strings by double quotes both left and right member must be embraced; never type spaces before and after == in IF statements and last but not least you coded after; instead of after : software is precision!

Report •

Related Solutions


Ask Question