Discussion:
If syntax for numeric values
(too old to reply)
Alan
2009-03-10 20:41:07 UTC
Permalink
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
T Lavedas
2009-03-10 21:20:39 UTC
Permalink
   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/
Pegasus
2009-03-10 21:26:42 UTC
Permalink
Post by 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
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.
Al Dunbar
2009-03-10 22:40:16 UTC
Permalink
Post by Pegasus
Post by 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
@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
Alan
2009-03-11 03:03:30 UTC
Permalink
Thank you all. Spaces were my problem. Alan
Alan
2009-03-12 01:09:52 UTC
Permalink
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
T Lavedas
2009-03-12 01:36:54 UTC
Permalink
OK.  This is driving me nuts. . . .
. . .
set retry=0
:_ProcNames
    if [%retry%]==[1] GoTo NewName
. . .
:NewName
     This code is a fragment of code in a for loop.  It was not
 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
=========
Alan
2009-03-12 02:05:36 UTC
Permalink
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

Loading...