Solved

MS-DOS SET Statement Withing the IF Clause

Posted on 2004-04-08
10
3,534 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 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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
Backup a Local Directory to the Network Using a BAT File 10 55
Rename files on Windows 10 54
Xcopy / Robocopy Question 7 74
Running Visio Macro from VBS File 3 124
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

732 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