Solved

MS-DOS SET Statement Withing the IF Clause

Posted on 2004-04-08
10
3,506 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
 
LVL 7

Expert Comment

by:4auHuk
ID: 10792102
:) sorry Steve.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

26 Experts available now in Live!

Get 1:1 Help Now