Solved

MS-DOS SET Statement Withing the IF Clause

Posted on 2004-04-08
10
3,531 Views
Last Modified: 2012-05-04
I have the following code:

@SET Description=BEFORE
@IF 0 EQU 0 (
@SET Description=AFTER
@ECHO The curent state is %Description% the IF statement.
)
@ECHO The curent state is %Description% the IF statement.
@PAUSE

When I run it, it returns:
The curent state is BEFORE the IF statement.
The curent state is AFTER the IF statement.

How do I make it return the second output only, in other words, how do make the first ECHO display the "AFTER" note?

Thanks,
Greg.
0
Comment
Question by:grishkin1
  • 2
  • 2
  • 2
  • +2
10 Comments
 
LVL 11

Expert Comment

by:lijunguo
ID: 10789996
"@IF 0 EQU 0"

since it's always true, so the code inside will be executed anyway. you may change it to

"@IF 0 EQU 1" or whatever, but not '0'. the code inside bracket won't be executed.

Lijun
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 50 total points
ID: 10792070
Try this:

@setlocal ENABLEDELAYEDEXPANSION

@SET Description=BEFORE
@IF 0 EQU 0 (
@SET Description=AFTER
@ECHO The curent state is !Description! the IF statement.
)
@ECHO The curent state is %Description% the IF statement.
@PAUSE

Good Luck,
Steve
0
 
LVL 7

Expert Comment

by:4auHuk
ID: 10792091
Hi grishkin1,
You need to enable delayed environment variable expansion for this to work.

From "set /?":

----start quote---
Finally, support for delayed environment variable expansion has been
added.  This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE.  See CMD /?

Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed.  The following example
demonstrates the problem with immediate variable expansion:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "%VAR%" == "after" @echo If you see this, it worked
    )

would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement.  So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal.  Similarly, the following example
will not work as expected:

    set LIST=
    for %i in (*) do set LIST=%LIST% %i
    echo %LIST%

in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:

    for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time.  If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "!VAR!" == "after" @echo If you see this, it worked
    )

    set LIST=
    for %i in (*) do set LIST=!LIST! %i
    echo %LIST%

----end quote----



This should work as you expect:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET Description=BEFORE
IF 0 EQU 0 (
    SET Description=AFTER
    ECHO The curent state is !Description! the IF statement.
)
ECHO The curent state is %Description% the IF statement.
ENDLOCAL
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 7

Expert Comment

by:4auHuk
ID: 10792102
:) sorry Steve.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 10792263
No problem. You are the master of delayed environment variable expansion.

Last night I was looking at one of your previous submissions to figure out how to utilize an environment variable in a set substrings.
0
 

Author Comment

by:grishkin1
ID: 10794934
Wow!  Thanks, Steve!  It worked!  You're da man! :)
0
 
LVL 11

Expert Comment

by:lijunguo
ID: 10795425
Hi all,
after reading the post, I think I misunderstood the question. Because English is not my first language. Sorry about my post.

Lijun
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10835306
Greetings grishkin1 - if you're all set here, can you close out this question?
See http:help.jsp#hs5 for assistance on closing.
Thanx!

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WINZIP: Zipping up a directory tree (and excluding .ZIP files) 18 94
Windows Bulk File Rename 3 128
dsquery command line assistance 2 122
WMIC Command LIne 5 56
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question