Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS-DOS SET Statement Withing the IF Clause

Posted on 2004-04-08
10
Medium Priority
?
3,556 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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