Check variable is number

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
firmindani
Posts: 1
Joined: 25 Oct 2012 08:38

Check variable is number

#1 Post by firmindani » 25 Oct 2012 13:38

I have a process that checks the output of the previous command is greater than 0.

for /f "delims=" %%a in ('sqlcmd -S host -E -h-1 -Q "SET NOCOUNT ON; SELECT MAX(Com_Num) AS MaxCompVal FROM status"') do @set MaxComp=%%a

if %MaxComp% > 0 goto FPass

:FPass
<Commands>

The above process runs successfully if the sqlcmd returns a number but sometimes the database is not available when its in maintenance and I am getting an error message. I want perform whether the MaxComp value is a number befor evaluating it.

In short I want to check a command vairable value is all numbers or not and based on the evaluation I want to do some action.

Thanks in advance.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Check variable is number

#2 Post by Ed Dyreen » 25 Oct 2012 14:41

Code: Select all

@echo off &setlocal enableDelayedExpansion

call :isNumber -5
call :isNumber  0
call :isNumber +5
call :isNumber notANumber
call :isNumber
set "var=50" &call :isNumber var
set "var=notANumber" &call :isNumber var

pause
exit

:isNumber
:: (
   set "_=" &set "_=!%~1!" ||set "_=%~1"
   ::
   set /a $err = 0
   ::
   if !_!.==. set /a $err = 1
   ::
   if !_! neq 0 set /a !_! %% !_! 2>nul ||set /a $err = 1
   ::
   if !$err! == 0 (echo. isNumber !_! ) else echo. NOTaNumber !_!
:: )
exit /b %$err%

Code: Select all

 isNumber -5
 isNumber 0
 isNumber +5
 NOTaNumber notANumber
 NOTaNumber
 isNumber 50
 NOTaNumber notANumber
Druk op een toets om door te gaan. . .

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Check variable is number

#3 Post by dbenham » 25 Oct 2012 15:05

This should generally work.

Code: Select all

setlocal enableDelayedExpansion
2>nul set /a "!MaxComp!" && if !MaxComp! gtr 0 goto FPass
The above could falsely report a number if the value is a valid numeric expression but not a number - something like "5+10".

The following will strictly test if the value is a valid decimal number:

Code: Select all

setlocal enableDelayedExpansion
2>nul set /a "test=!MaxComp!" || set "test="
if !test!==!maxComp! if !MaxComp! gtr 0 goto FPass


Dave Benham

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Check variable is number

#4 Post by trebor68 » 25 Oct 2012 15:24

One way to check if a variable is not negativ is following code.

Code: Select all

@echo off
for /l %%a in (-15,1,15) do echo %%a| findstr /r "^[1-9][0-9]*$">nul && echo %%a


This code work perfectly when the number have not any characters as the numbers.
i.E "2", "23", "234", "2345", "23456"
But not "2.345" or "23,456"

To understand the FINDSTR command:
caret -- is the the beginn of the value
[1-9] -- one number from 1 to 9 for the first character
[0-9]* -- more numbers from 0-9 posibble or not more numbers
$ -- is the end of the value

Here the code block for the problem:

Code: Select all

...
echo %MaxComp%| findstr /r "^[1-9][0-9]*$">nul && goto :FPass
...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Check variable is number

#5 Post by foxidrive » 25 Oct 2012 19:41

This should work:

firmindani wrote:I have a process that checks the output of the previous command is greater than 0.

for /f "delims=" %%a in ('sqlcmd -S host -E -h-1 -Q "SET NOCOUNT ON; SELECT MAX(Com_Num) AS MaxCompVal FROM status"') do @set MaxComp=%%a

for /f "delims=0123456789A" %%a in ("%MaxComp%") do if "%%a"=="A" if not "%MaxComp%"=="0" goto :FPass
echo MaxComp variable fails comparison
pause
goto :EOF


:FPass
<Commands>


abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Check variable is number

#6 Post by abc0502 » 26 Oct 2012 05:33

Edit:
This is for a one digit output number Only in range of 1 to 9

why not using

Code: Select all

 For %%a in (1 2 3 4 5 6 7 8 9) Do ( IF "%MaxComp%" == "%%a" IF not "%MaxComp%" == "0" ( Do_Command ) )
Last edited by abc0502 on 26 Oct 2012 06:11, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Check variable is number

#7 Post by foxidrive » 26 Oct 2012 05:52

abc0502 wrote:why not using

Code: Select all

 For %%a in (1 2 3 4 5 6 7 8 9) Do ( IF "%MaxComp%" == "%%a" IF not "%MaxComp%" == "0" ( Do_Command ) )


This assumes the number is from 1 to 9 and nothing else, right?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Check variable is number

#8 Post by abc0502 » 26 Oct 2012 05:56

yes, he check if the output is greater than "0"

Edit:
and there is no need for the 2nd If that check if the output is not equal to zero,
as it won't be in the range 1 to 9

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Check variable is number

#9 Post by foxidrive » 26 Oct 2012 06:06

The OP hasn't stated that the number is from 0 to 9. I assumed when he says "variable is all numbers' that the number can be 2 or 3 digits etc.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Check variable is number

#10 Post by abc0502 » 26 Oct 2012 06:10

Oh, i didn't thought about that , what came in my mind that it could be a number as an error level "one digit" not a number that could consist of more that one digit.

Post Reply