How to check if a parameter (or variable) is a number in a Batch script

后端 未结 10 540
南方客
南方客 2020-12-01 08:02

I need to check if a parameter that is passed to a Windows Batch file is a numeric value or not. It would be good for the check to also works for variables.

I found a

10条回答
  •  时光取名叫无心
    2020-12-01 09:02

    if errorlevel accepts only numeric values which can be used for a checks:

    set test_var=001
    ( 
      (if errorlevel %test_var% break ) 2>nul
    )&&(
      echo %test_var% is numeric
    )||(
      echo %test_var% is NOT numeric
    )
    
    set test_var=001X
    ( 
      (if errorlevel %test_var% break ) 2>nul
    )&&(
      echo %test_var% is numeric
    )||(
      echo %test_var% is NOT numeric
    )
    

    the first check will pass, the second not. Though the script above would not handle unary + - if you want to handle this case too check - isInteger.bat

提交回复
热议问题