Search code examples
variablesif-statementbatch-filesetfalse-positive

Batch check if a variable is negative


i have a little problem in batch and i don't find a way to solve it ! I'm working on a point system and just need a string that checks if a variable is negative. It would be something like this :

set /p points=10
set /p price=15
set /a result=%points%-%price%
if result < 0 ( goto error ) else ( goto done)

:error
echo You have not enough Points !

:done
echo Transaction Successfully Finished !

The point is, i want to redirect the user to a different label depending on the fact that the variable that define the money (or something like that) is positive or negative.

EDIT : 4 years later, thanks for having helped a poor script kiddo, i'm studying IT now :D


Solution

  • I'm a bit of a batch file noob (but an aspiring enthusiast!) so please excuse me if my answer isn't helpful lol.

    By typing if /? at the command line you'll see these comparison operators:

    EQU - equal
    NEQ - not equal
    LSS - less than
    LEQ - less than or equal
    GTR - greater than
    GEQ - greater than or equal
    

    In this case I would use this syntax:

    if result LSS 0 (goto :error) else (goto :done)
    

    I hope this helps!