[Solved] SETLOCAL EnableDelayedExpansion script not working

Score
0
Vote Up
February 6, 2012 at 12:52:36 Pacific
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?


Jump to Best Answer ↓   Report •


#1
Vote Down
Score
0
Vote Up
February 6, 2012 at 13:42:52 Pacific

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


Reply ↓  Report •

#2
Vote Down
Score
0
Vote Up
February 6, 2012 at 13:48:00 Pacific

@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!

Reply ↓  Report •

Reply to Message Icon Start New Discussion
Related Posts

« [Solved] I SIMPLY want to download... How to finis batch script... »