Numbers and basic math in BATCH

November 11, 2009 at 18:18:58
Specs: Windows XP
Hi,

I'm getting confused on trying to use numeric variables in BAT files. I've seen these names:

variable
_variable
%variable
%variable%

Specifically, I'd like to:
- set a variable to zero
- compare a variable with another.
- use SET /A to do basic math.
- echo the variable.

Right now I'm more dazed than usual in trying to get this straight. I'd greatly appreciate an example on how to do this:

set /A var1=0
set /A var2=1
if var1==var2 then print equal
increment var1 by 1
add var1 and var2 giving var3
echo "var1=" var1


See More: Numbers and basic math in BATCH

Report •


#1
November 11, 2009 at 19:11:13
@echo off & setLocal EnableDELAYedExpansion

set /A var1=0
set /A var2=1
if !var1!==!var2! echo equal
set /a var1+=1
set /a var3=!var1!+!var2!
echo "var1=" !var1!
echo "var3" !var3!


=====================================
Helping others achieve escape felicity

M2


Report •

#2
November 12, 2009 at 00:47:38
Thanks! I can add 2 plus 2 now.

Can you give some detail on these:

_variable
%variable
%variable%

Are they used in another language?

Or are they used in batch to do something different than what I'm trying to do?


Report •

#3
November 12, 2009 at 00:58:42
The first 2 may be viable in other laguages.

The third is legit in batch.


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
November 12, 2009 at 05:49:58
To expand on M2's answer:

variable is just the word 'variable' (but see below).

%variable% gets "expanded" i.e. translated to its value and literally substituted in the command line before the interpreter even gets to see it.

!variable! get passed as that to the interpreter, which then does the expansion itself. To use this, you first need to enable it for the current batch file by adding this command at (or near) the start:

setlocal enabledelayedexpansion

There is one exception to the above rule, which is when you use the SET /A command. A plain SET command (and all other commands) will not expand the value of a variable, but will treat the variable name literally. But SET /A knows that it has to work with numbers, and only numbers. Therefore, when it finds a word that's not a number, it treats it as a variable. Thus you don't have to say:

SET /A z = !x! + !y!

but you can just say

SET /A z = x + y

Note also that I've got spaces around the = sign. Normally you shouldn't do that, or you'll be defining a variable whose name ends with a space and whose value begins with a space. But SET /A understands that the space around = is not to be taken literally.


Report •

#5
November 12, 2009 at 06:04:12
The second is used in FOR loops while not in a script, or for positional parameters while in a script.

Report •

#6
November 12, 2009 at 22:18:38
regarding the double percents and Razor's observation,
what i'd like to know is, who the hell uses "for" at command prompt anymore? it would be really really nice if *someone* at MS would just do away with the extra % which (pls corr me if im wrong) no one uses anyway. i think it's leftover from DOS. It mainly just adds to confusion at this point.
end of rant/rave. sorry.

Report •

#7
November 13, 2009 at 01:57:27
I use 'for' interactively, e.g. if I want to prefix my filenames I
just quickly type
for %f in (*) do @ren %f xxx%f
.
However, I don't see what that has got to do with the
requirement to type %% in a script. It seems that in a script if
you type %f the interpreter will attempt to expand a variable
beginning with f and then get confused when it doesn't find a
terminating %. However, they fixed that issue on the
command line, so why not fix it in a script too? Anyway, I
think we are talking to a wall, because MS are not really
interested in enhancing cmd.exe any more.

Report •

#8
November 13, 2009 at 05:11:42
klint: It seems that in a script if
you type %f the interpreter will attempt to expand a variable
beginning with f and then get confused when it doesn't find a
terminating %.

Close, but not quite. It's looking for a few things:
- A digit (0-9)
- Another %
- An *
- A word ending with %
- A word starting with %~ and ending in a digit

If the script processor doesn't find one of these, it simply drops the errant % sign and continues processing the line.


Report •


Ask Question