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=beforeC:\>if "before" == "before" (
set var=after;
if !var! == "after"
)C:\>
What am I doing wrong here?
if "!var!"=="after;" @echo if you see me, it worked1) 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;"
@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!
| « [Solved] I SIMPLY want to download... | How to finis batch script... » |