Home All Groups Group Topic Archive Search About

If syntax for numeric values

Author
10 Mar 2009 8:41 PM
Alan
Hi.  I have a bonehead question.  I'm just trying to do a simple
cmd script using an IF statement.  Why doesn't the code below work?

                   Thanks, Alan

set retry = 0

if retry = 0 echo Hello

Author
10 Mar 2009 9:20 PM
T Lavedas
On Mar 10, 4:41 pm, Alan <jalantho***@verizon.net> wrote:
>    Hi.  I have a bonehead question.  I'm just trying to do a simple
> cmd script using an IF statement.  Why doesn't the code below work?
>
>                    Thanks, Alan
>
> set retry = 0
>
> if retry = 0 echo Hello

Type IF/? at the command prompt.  Also, don't use spaces in batch,
unless they are absolutely necessary.  For example, your SET statement
creates a variable named 'retry ' (no quotes).  That is, the name
includes the space before the equal sign.  In batch, the IF test is C-
like in that the equivalence operator is == (two equal signs).
Finally, an environment variable is indicated by enclosing its name in
percent signs.  So, the correct syntax is ...

set retry=0

if %retry%==0 echo Hello

Unfortunately, there is still a problem here, but it's not directly
related to the syntax.  Rather, the subtler than that.  The problem
will only arise in the instance that the variable RETRY fails to be
defined at teh time of the test.  In that case, the command looks like
this ...

if ==0 echo Hello

which does create a syntax error.  A common solution is to add one or
more dummy characters to both sides of the equal signs, as in ...

if [%retry%]==[0] echo Hello

then in the event RETRY is undefined (empty) the statement is
evaluated as ...

if []==[0] echo Hello

which remains syntactically correct and evaluates as a false
condition.

HTH,

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
Author
10 Mar 2009 9:26 PM
Pegasus
"Alan" <jalantho***@verizon.net> wrote in message
news:592f12fa-df6f-402b-8622-32232cd27391@z1g2000yqn.googlegroups.com...
>   Hi.  I have a bonehead question.  I'm just trying to do a simple
> cmd script using an IF statement.  Why doesn't the code below work?
>
>                   Thanks, Alan
>
> set retry = 0
>
> if retry = 0 echo Hello

Try this:

@echo off
set retry=0
if retry==0 echo Hello

Note the absence of spaces around the = and the doubling of the = symbol.
Author
10 Mar 2009 10:40 PM
Al Dunbar
Show quote Hide quote
"Pegasus" <n***@microsoft.com> wrote in message
news:ei37nbcoJHA.5360@TK2MSFTNGP03.phx.gbl...
>
> "Alan" <jalantho***@verizon.net> wrote in message
> news:592f12fa-df6f-402b-8622-32232cd27391@z1g2000yqn.googlegroups.com...
>>   Hi.  I have a bonehead question.  I'm just trying to do a simple
>> cmd script using an IF statement.  Why doesn't the code below work?
>>
>>                   Thanks, Alan
>>
>> set retry = 0
>>
>> if retry = 0 echo Hello
>
> Try this:
>
> @echo off
> set retry=0
> if retry==0 echo Hello
>
> Note the absence of spaces around the = and the doubling of the = symbol.

Or try this:

    @echo off
    set/a retry = 0
    if "%retry%" EQU "0" echo/Hello

/Al
Author
11 Mar 2009 3:03 AM
Alan
Thank you all.  Spaces were my problem.     Alan
Author
12 Mar 2009 1:09 AM
Alan
OK.  This is driving me nuts. . . .

     I have the following code in a large CMD file:

.. . .
set retry=0

:_ProcNames
    if [%retry%]==[1] GoTo NewName
.. . .
:NewName

     This code is a fragment of code in a for loop.  It was not
working, so I wrote out to a log file.  It shows:

set retry=0
if [] == [1] GoTo NewName

I already checked the spacing.

      HOWEVER, just to be that much more irritating, when I put the
code fragment (copied from the original) as follows in a separate CMD
file, it works!

set retry=0

:_ProcNames
    if [%retry%]==[1] GoTo NewName
    echo Not retrying . . .
    set retry=1
    GoTo _ProcNames

:NewName
echo Processing NEW name . . .

PAUSE
exit

     Any idea what is going on???????      Thanks in advance, Alan
Author
12 Mar 2009 1:36 AM
T Lavedas
On Mar 11, 8:09 pm, Alan <jalantho***@verizon.net> wrote:
Show quoteHide quote
> OK.  This is driving me nuts. . . .
>
>      I have the following code in a large CMD file:
>
> . . .
> set retry=0
>
> :_ProcNames
>     if [%retry%]==[1] GoTo NewName
> . . .
> :NewName
>
>      This code is a fragment of code in a for loop.  It was not
> working, so I wrote out to a log file.  It shows:
>
>  set retry=0
>  if [] == [1] GoTo NewName
>
> I already checked the spacing.
>
>       HOWEVER, just to be that much more irritating, when I put the
> code fragment (copied from the original) as follows in a separate CMD
> file, it works!
>
> set retry=0
>
> :_ProcNames
>     if [%retry%]==[1] GoTo NewName
>     echo Not retrying . . .
>     set retry=1
>     GoTo _ProcNames
>
> :NewName
> echo Processing NEW name . . .
>
> PAUSE
> exit
>
>      Any idea what is going on???????      Thanks in advance, Alan

I was stumped for a minute until I found a clue.  The clue is your
statement that this "code in a for loop"!  Then I knew what the
problem is. It is that environment variables are evaluated only once
when the code of a FOR loop is first parsed, unless a special
formulation is employed.  This is called 'delayed expansion'.  It is
explained in, of all places, the help to the SET statement, though
there is a reference in the SETLOCAL statements help (but not a word
in the help to the FOR):

quote
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

Invoking delayed expansion requires the following rather wordy
statement ...

setlocal enabledelayedexpansion

So try this and see if it doesn't fix things...

setlocal enabledelayedexpansion
for ... in (...) do (...
set retry=0

:_ProcNames
    if [!retry!]==[1] GoTo NewName
    echo Not retrying . . .
    set retry=1
    GoTo _ProcNames

:NewName
echo Processing NEW name . . .

)

Tom Lavedas
=========
Author
12 Mar 2009 2:05 AM
Alan
Tom,
         There is already a "setlocal enabledelayedexpansion"
statement at the beginning of the CMD file.  I just did not know it
was important.

    However, when I changed "if [%retry%]==[1] GoTo NewName" to "if [!
retry!]==[1] GoTo NewName" then it appears to work.  I thought the
output log would show something like "if [0]==[1].  However, it showed
"if [!retry!]==[1]" instead.  That appears to indicate that it is
being evaluated now.  The branching did work.

      Thank you very much.  If you know where I can buy a decoder
ring, please let me know.

Alan