Page 1 of 1
Check variable is number
Posted: 25 Oct 2012 13:38
by firmindani
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.
Re: Check variable is number
Posted: 25 Oct 2012 14:41
by Ed Dyreen
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. . .
Re: Check variable is number
Posted: 25 Oct 2012 15:05
by dbenham
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
Re: Check variable is number
Posted: 25 Oct 2012 15:24
by trebor68
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
...
Re: Check variable is number
Posted: 25 Oct 2012 19:41
by foxidrive
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>
Re: Check variable is number
Posted: 26 Oct 2012 05:33
by abc0502
Edit:This is for a one digit output number Only in range of 1 to 9why 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 ) )
Re: Check variable is number
Posted: 26 Oct 2012 05:52
by foxidrive
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?
Re: Check variable is number
Posted: 26 Oct 2012 05:56
by abc0502
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
Re: Check variable is number
Posted: 26 Oct 2012 06:06
by foxidrive
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.
Re: Check variable is number
Posted: 26 Oct 2012 06:10
by abc0502
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.