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

后端 未结 10 549
南方客
南方客 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 08:44

    You could try this. The variable passed is for example var and %var% is equal to 500.

    set /a varCheck=%var%
    if %varCheck% == %var% (goto :confirmed) else (exit /B)
    exit /B
    
    :confirmed
    :: You can use %var% here, and it should only be executed if it is numerical!
    

    if %var% is equal to e.g. a3453d, then it would set varCheck to be 0, and because 0 is not equal to a3453d, then it will exit batch processing.

    (The exit on line 3 is just in case the if statement decides not to execute for some reason ... XD.)

提交回复
热议问题