Estoy tratando de obtener un archivo por lotes para probar si un número está entre dos números, pero solo está buscando el primer número. Estoy usando el operador "y" pero no parece estar funcionando. Aquí está el código:

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.
)
0
Brett 26 jun. 2017 a las 00:00

3 respuestas

La mejor respuesta

El operador lógico and no existe en BAT,

Tienes que hacer una segunda prueba if contra tu variable:

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

EDITAR:

 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)
     )
2
SachaDee 25 jun. 2017 a las 21:28
:: 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
0
earthzen 25 jun. 2017 a las 22:50

Ver ... Encadenamiento de comandos IF (Y). https://ss64.com/nt/if.html

Aquí hay una implementación para operadores AND / OR / XOR / NOT en una declaración IF del Prof. Timo Salmi http://www.netikka.net/tsneti/info/tscmd120.htm

Otro buen hilo Operadores lógicos ("y", "o") en lote de DOS

0
earthzen 25 jun. 2017 a las 21:25