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

The easiest for positive integers should be:

IF 1%1 NEQ +1%1 echo Notnumeric!

If negative numbers (hyphen) are also to be considered, this will work

SET number=%1
if 1%1 EQU +1%1 echo positive number
if %1==-%number:-=% echo negative number

Learned from this article here


for ± integers (test also for leading zero):

echo(%~1|findstr "^[-][1-9][0-9]*$ ^[1-9][0-9]*$ ^0$">nul&&echo numeric||echo not numeric

SET "var="&for /f "delims=0123456789" %%i in ("%1") do set var=%%i
if defined var (echo %1 NOT numeric) else (echo %1 numeric)

Replace %1 with %yourvarname% as appropriate


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.)