Rik Telner
Rik Telner

Reputation: 279

Verify that a variable is a number

How does one verify that a variable is a number in a batch file?

1, 2, 4, 5, 10934, 495832945, 4395893428 ->  true
Apple, Orange, Animal, Red, Sky, Leaf -> false

Upvotes: 2

Views: 111

Answers (3)

npocmaka
npocmaka

Reputation: 57252

echo %variable%| FINDSTR /R /X " *[0-9]* *" >nul 2>&1 && echo IT IS A NUMBER
echo %variable%| FINDSTR /R /X " *[0-9]* *" >nul 2>&1 || echo IT IS NOT A NUMBER

Upvotes: 1

David Ruhmann
David Ruhmann

Reputation: 11367

From http://www.robvanderwoude.com/sourcecode.php?src=isnumber_nt [ Alt Link ]

:: Positive decimal
SET RC=10
FOR /F "tokens=1 delims=0123456789" %%A IN ("%Value%") DO SET RC=0
IF %RC% EQU 10 EXIT /B 10

Upvotes: 1

Rik Telner
Rik Telner

Reputation: 279

Answer found on StackOverflow.com

set /p Input1=Enter number: 
set /a Input2=%Input1% * 1
if "%Input2%"=="0" echo It is not a number.

It doesn't work with 0 to 0.9 numbers.

Upvotes: 1

Related Questions