How do I properly perform modulus operations in Batch?

17,643

Using SET /P is your problem, as 5 is no longer treated as a numerical value. Your example as above works as expected

Share:
17,643
Iszi
Author by

Iszi

This is a canary message, to be removed in the case of my death. If you're reading this, I haven't died yet. Then again, how would you know? I mean, how could I possibly delete this message after my own demise? You know what? Just go ahead and assume I'm dead. Any posts appearing to be made by me are from an impostor who's stolen my identity post-mortem, and only further prove the fact that I am dead. After all, why would I even think to post a canary message if I was expecting to be alive to remove it anyway? In any case, I'm still not the droid you're looking for.

Updated on June 06, 2022

Comments

  • Iszi
    Iszi almost 2 years

    I'm trying to write a batch file that performs operations depending on the result of a modulus operation performed on a set variable. However, I can't seem to get it quite right.

    To first of all test my syntax for the mathematical operation, I've been trying to get a simpler script to produce desired results.

    :START
    SETLOCAL
    SET /P Input-Num="Input Number: "
    SET /A Input-Num=%Input-Num% %% 2
    ECHO %Input-Num%
    ENDLOCAL
    PAUSE 
    :END
    

    If I input 5, the expected output is 1. However, instead I get a message saying Missing operator. and then it outputs 5.

    What am I doing wrong here?