1

I am trying to get a batch file to test if a number is between two numbers but it is only checking for the first number. I am using the "and" operator but it doesn't seem to be working. Here's the code:

REM Used to keep variable for duration of script
setlocal enabledelayedexpansion

cls
@echo off 

echo Math Experts Commands
set /p loop=Loop how many times: 

for /L %%A IN (1,1,!loop!) DO (
    REM Request the first number
    echo Enter your first digit you wish to add

    REM Set first user input to "num1"
    set /p num1=

    REM Request the second number
    echo Enter the second digit you wish to add the first by

    REM Set second usr input to "num2"
    set /p num2=

    REM Add the 2 values then set the outcome to "total"
    set /a total=num1+num2

    REM Clear the screen
    cls

    if !total! leq 4 (
    echo ####################################################################################
    echo #    Hosts Req:      #      Mask:      #      Netmask:      #      Max Supported:  #                                                  
    echo #    !total!                      /30           255.255.255.252                4         #
    echo ####################################################################################
    echo.
    ) else if !total! gtr 4 and leq 8 (   REM <----WHERE THE PROBLEM IS. 
    echo ####################################################################################
    echo #    Hosts Req:      #      Mask:      #      Netmask:      #      Max Supported:  #                                                  
    echo #    !total!                      /29            255.255.255.248               8        #
    echo ####################################################################################
    echo. 
    )
    )

    REM Pause the console
    pause
    echo.
)
Brett
  • 43
  • 1
  • 8

3 Answers3

2

The logical and operator don't exist in BAT,

You have to make a second if test against you variable :

else (if !total! gtr 4 if !total! leq 8 (....))

EDIT :

 else (
     if !total! gtr 4 if !total! leq 8 (echo between 4 and 8)
     if !total! gtr 16 if !total! leq 32 (echo between 16 and 32)
     )
SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • Thanks, that worked but now i face a new problem in that i need to do the same thing a few more times with increasing numbers. (ex. >8 & <=16, >16 & <=32, ect.) – Brett Jun 25 '17 at 21:18
  • 1
    @Brett - Your options are copy/pasting a whole lot or figuring out the math needed to dynamically fill in a template. – SomethingDark Jun 25 '17 at 21:24
  • Thank you so much, It Worked! I've been working on this for hours and this might be the only thing i did't try – Brett Jun 25 '17 at 21:27
1

See...Chaining IF commands (AND). https://ss64.com/nt/if.html

Here is an implementation for AND/OR/XOR/NOT operators in an IF statement by Prof. Timo Salmi   http://www.netikka.net/tsneti/info/tscmd120.htm

Another good thread Logical operators ("and", "or") in DOS batch

earthzen
  • 21
  • 1
  • Please include appropriate content from those websites to answer the question rather than just linking. Other websites may go away, making them no longer useful as an answer. – user3486184 Jun 26 '17 at 00:39
1
:: I used code from here; http://www.robvanderwoude.com/battech_booleanlogic.php
:: Requirements
:: Test for x >8 & <=16, >16 & <=32, etc
:: I only implemented the first two tests
:: A loop could condense the code maybe?
::
:: Reference: IF statement logical operators
:: EQU : Equal
:: NEQ : Not equal

:: LSS : Less than <
:: LEQ : Less than or Equal <=

:: GTR : Greater than >
:: GEQ : Greater than or equal >=

:: Initialize Variables
set Comp1=0
set Comp2=0

:: Set a test case
set X=18
echo x=%x%
echo.

:: Test "8 < x <= 16"
IF %x% GTR 8 (SET Cond1=1) ELSE (SET Cond1=0)
IF %x% LEQ 16 (SET Cond2=1) ELSE (SET Cond2=0)
SET /A "ResultAND = %Cond1% & %Cond2%"
IF %ResultAND% EQU 1 (SET Comp1=1) ELSE (SET Comp1=0)
IF %Comp1% EQU 1 (ECHO %x% is between 9 and 16) ELSE (ECHO %x% is NOT between 9 and 16)

:: Test "8 < x <= 16 OR 16 < x <= 32"
IF %x% GTR 16 (SET Cond1=1) ELSE (SET Cond1=0)
IF %x% LEQ 32 (SET Cond2=1) ELSE (SET Cond2=0)
SET /A "ResultAND = %Cond1% & %Cond2%"
IF %ResultAND% EQU 1 (SET Comp2=1) ELSE (SET Comp2=0)
IF %Comp2% EQU 1 (ECHO %x% is between 17 and 32) ELSE (ECHO %x% is NOT between 17 and 32)

SET /A "ResultOR = %Comp1% | %Comp2%"
IF %ResultOR% EQU 1 (ECHO %x% is between 9 and 16 OR between 17 and 32) ELSE (ECHO %x% is NOT between 9 and 16 OR between 17 and 32)

:: Logical Operator Tests
::SET /A "ResultAND = %Cond1% & %Cond2%"
::IF %ResultAND% EQU 1 (ECHO AND: TRUE) ELSE (ECHO AND: FALSE)

::SET /A "ResultOR = %Cond1% | %Cond2%"
::IF %ResultOR% EQU 1 (ECHO OR:  TRUE) ELSE (ECHO OR:  FALSE)

::SET /A "ResultXOR = %Cond1% ˆ %Cond2%"
::IF %ResultXOR% EQU 1 (ECHO XOR: TRUE) ELSE (ECHO XOR: FALSE)

pause
earthzen
  • 21
  • 1