# Simple BATCH math



## madd74

I am writing a .BAT file for my job function. It is extremely nice minus one simple thing I am attempting to do, math. How would one write the following for a BAT file?

--------------------
set /p hour= Number of hours on hold: 
if '%hour%' =< 1 set gramtime="hour"
if '%hour%' =< 1 set gramtime="hours"
echo You are on hold for %hour% %gramtime%
--------------------

Everything is working except "gramtime" is not being set to anything because this gives an error message that "= was unexpected at this time" and the .BAT exits. I also attempted

--------------------
@echo off
set hour=
echo 1
set /p hour= Number of hours on hold: 
echo 2
if '%hour%'=="1" goto justhour
echo 3
set gramtime=hours
echo 4
goto cont
echo 5
:justhour
echo 6
set gramtime=hour
echo 7
goto cont
echo 8
:cont
echo You are on hold for %hour% %gramtime%
--------------------

Now the thing I noticed with this, is that the ECHO display 1,2,3,4 each time, meaning the GOTO is not being triggered when I would put 1 in as my input. I do not ever see 5 or 8 (because I never will ) and I do not ever see 6 (even though my "You are on hold for..." would display 1 if I entered in 1)

So my output is always "You are on hold for X hours"

Thanks in advance for any help!


----------



## Squashman

Code:


Two new switches have been added to the SET command:

    SET /A expression
    SET /P variable=[promptString]

The /A switch specifies that the string to the right of the equal sign
is a numerical expression that is evaluated.  The expression evaluator
is pretty simple and supports the following operations, in decreasing
order of precedence:

    ()                  - grouping
    ! ~ -               - unary operators
    * / %               - arithmetic operators
    + -                 - arithmetic operators
    << >>               - logical shift
    &                   - bitwise and
    ^                   - bitwise exclusive or
    |                   - bitwise or
    = *= /= %= += -=    - assignment
      &= ^= |= <<= >>=
    ,                   - expression separator


----------

