locked
Windows Command Prompt: IF Command Bug RRS feed

  • Question

  • Hello.

    I was writing a batch file to make use with a third-party hardware info program used to check the processor's temperature, but while I was writing it, I found a bug with the IF command. Here are a few examples:

    C:\Users\CreeperGabe\AppData\Local>if '89.0' LSS '180.0' echo yes

    C:\Users\CreeperGabe\AppData\Local>if '128.0' LSS '180.0' echo yes

    yes

    C:\Users\CreeperGabe\AppData\Local>

    -------------------------------------------------------------------------------

    C:\Users\CreeperGabe\AppData\Local>if '89.0' GEQ '180.0' echo yes

    yes

    C:\Users\CreeperGabe\AppData\Local>if '128.0' GEQ '180.0' echo yes

    C:\Users\CreeperGabe\AppData\Local>

    I've been trying to work around this problem many times, but yet fail. Here is my batch file:

    rem if '%1' LEQ '99.9' (
    rem   goto normal
    rem ) else (
    'if '%1' LSS '180.0' (
    '  goto normal
    ')
    if '%1' GEQ '100.0' (
      if '%1' LSS '180.0' (
        goto normal
      ) else (
        goto alert
      )
    )
     
    :normal
    choice /c QZ /n /cs /t 3 /d Q /m "System is normal at %1øF..."
    exit

    :alert
    title Warning!
    echo System is overheating at %1øF! Turn the laptop or tilt it upwards from the   left, then press a key.
    pause >nul

    It's still not finished. Is there an available patch for this that I can install, or anything that will work? This is really 'bug'ging me. I'd love to use QuickBasic for MS-DOS 7.1, but I am running an x64 based system, therefore, I can't. Until I find an answer, I am going to make it tell me the temperature every 10 minutes.

    Tuesday, June 10, 2014 5:37 PM

Answers

  • Not a bug. You are comparing strings, not numbers.

    Remove the single quotes (') and decimal points:


    C:\>if 89 LSS 180 echo yes
    yes
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by CreeperGabe Sunday, June 15, 2014 1:18 PM
    Tuesday, June 10, 2014 5:57 PM
  • It is pretty easy to truncate ...

      for /f "delims=." %%N in ("%1") do set WholeNumber=%%N

    Then use the environment variable (%or whatever you choose to name it) in place of %1 in the IF test (still without the quotes).

    If the decimal part is always one digit, you could add some logic to decide if it were greater or less than 5, but I personally don't think it's worth it.  If you want to try, modify the FOR statement to collect that part, something like this. 

      for /f "tokens=1,2 delims=." %%N in ("%1") do set WholeNumber=%%N& set Fraction=%O

    I'll leave the logic of the comparison to you, since it is nearly identical to what you did.

    OR, you could shift everything to the left by one digit (*10) and test whole numbers again, but only if the input can contain one and only one digit after the decimal point ...

      for /f "tokens=1,2 delims=." %%N in ("%1") do set /a NewNumber=%%N * 10 + %O

    Then you test values are increased by a factor of 10.

    HTH


    Tom Lavedas


    • Edited by Tom Lavedas Wednesday, June 11, 2014 3:18 AM to add an "a" after the slash in the SET part of the statement
    • Marked as answer by Bill_Stewart Friday, June 13, 2014 9:39 PM
    Wednesday, June 11, 2014 3:17 AM

All replies

  • Not a bug. You are comparing strings, not numbers.

    Remove the single quotes (') and decimal points:


    C:\>if 89 LSS 180 echo yes
    yes
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by CreeperGabe Sunday, June 15, 2014 1:18 PM
    Tuesday, June 10, 2014 5:57 PM
  • I'm using decimal points because the temperature reading from the program includes one. Plus, I'm sending arguments to the batch file, so I have to use %1.
    Tuesday, June 10, 2014 9:09 PM
  • With decimal points, cmd's if command thinks you're comparing strings. You can't coerce it into comparing them as numbers unless you remove the decimal points first.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 10, 2014 9:17 PM
  • Is there any possible way I can truncate or round %1 to the nearest whole with commands then?
    • Edited by CreeperGabe Tuesday, June 10, 2014 9:22 PM Specifics
    Tuesday, June 10, 2014 9:21 PM
  • Search for "batch file rounding" to see some possibilities.

    This would be simpler in PowerShell.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 10, 2014 9:25 PM
  • It is pretty easy to truncate ...

      for /f "delims=." %%N in ("%1") do set WholeNumber=%%N

    Then use the environment variable (%or whatever you choose to name it) in place of %1 in the IF test (still without the quotes).

    If the decimal part is always one digit, you could add some logic to decide if it were greater or less than 5, but I personally don't think it's worth it.  If you want to try, modify the FOR statement to collect that part, something like this. 

      for /f "tokens=1,2 delims=." %%N in ("%1") do set WholeNumber=%%N& set Fraction=%O

    I'll leave the logic of the comparison to you, since it is nearly identical to what you did.

    OR, you could shift everything to the left by one digit (*10) and test whole numbers again, but only if the input can contain one and only one digit after the decimal point ...

      for /f "tokens=1,2 delims=." %%N in ("%1") do set /a NewNumber=%%N * 10 + %O

    Then you test values are increased by a factor of 10.

    HTH


    Tom Lavedas


    • Edited by Tom Lavedas Wednesday, June 11, 2014 3:18 AM to add an "a" after the slash in the SET part of the statement
    • Marked as answer by Bill_Stewart Friday, June 13, 2014 9:39 PM
    Wednesday, June 11, 2014 3:17 AM