Solved

MS-DOS SET Statement Withing the IF Clause

Posted on 2004-04-08
10
3,523 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now