How to check if a parameter (or variable) is a number in a Batch script
SET "var="&for /f "delims=0123456789" %%i in ("%1") do set var=%%iif defined var (echo %1 NOT numeric) else (echo %1 numeric)
Replace %1
with %yourvarname%
as appropriate
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
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.)