1

Simply asked, I need to check if a variable is numerical. I'm aware of the ability of:

set /a variable1=%variable%

setting non numerical strings to 0, but i need to be able to have 0 as an intiger as well as negative numbers.

This will be run very often, so a fast script is preferred. I've tried to echo the variable into a .txt, and use a for loop to scan through and return an error if anything other than 0-9 is detected, but the script is excessively long running, and frankly is a mess.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Bloodied
  • 1,004
  • 7
  • 20
  • Can you edit the question to include the code you used to check if an integer was provided by the user? You were probably close. – SomethingDark Oct 21 '15 at 18:22
  • go to google and type in: batch file is numeric - or do it in the search bar in the upper right hand corner of this page.... – Sorceri Oct 21 '15 at 18:29
  • 1
    Have you tried something like the [for statement from this post](http://stackoverflow.com/questions/17584282/how-to-check-if-a-parameter-or-variable-is-numeric-in-windows-batch-file), or the [InStr method](http://superuser.com/questions/404338/check-for-only-numerical-input-in-batch-file) mentioned there as well? – u8it Oct 21 '15 at 18:44
  • 1
    I'm pretty sure 'set /a v=%v%' can handle negative values as well. So pretty much anything numeric will return something other than zero except zero itself. So why not just test for zero before hand and then nest this within that if statement? So the logic would go something like this... _(If zero, then numeric, else (if set /a v=%v% --> zero, then non-numeric, else numeric))_ – u8it Oct 21 '15 at 19:00
  • [This answer](http://stackoverflow.com/a/17585404/1012053) to an *almost* identical question solves your problem. – dbenham Oct 22 '15 at 01:41
  • P57 I don't know why i didnt think of that... – Bloodied Oct 22 '15 at 16:27

3 Answers3

2

As mentioned in question17584282

The easiest for digits should be:

IF %1 NEQ +%1 echo Notnumeric!

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

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

Learned from https://www.itprotoday.com/compute-engines/jsi-tip-9692-how-can-batch-script-determine-if-variable-or-parameter-integer

Alim Özdemir
  • 2,396
  • 1
  • 24
  • 35
1

You could do something to this affect. Remove all numbers. If anything is left over it is not an integer. Not saying this is perfect but it is a step in the right direction.

set "tempvar="
FOR /F "tokens=* delims=-0123456789" %%G IN ("%variable1%") DO SET "tempvar=%%G"

IF DEFINED tempvar echo NOT AN INTEGER
Squashman
  • 13,649
  • 5
  • 27
  • 36
0
@echo off 

:isInterer  input [returnVar] 
setlocal enableDelayedexpansion 
set "input=%~1" 

if "!input:~0,1!" equ "-" (
    set "input=!input:~1!"
) else (
    if "!input:~0,1!" equ "+" set "input=!input:~1!"
)

for %%# in (1 2 3 4 5 6 7 8 9 0) do ( 
        if not "!input!" == "" ( 
                set "input=!input:%%#=!" 
    )         
) 

if "!input!" equ "" ( 
        set result=true 
) else ( 
        set result=false 
) 

endlocal & if "%~2" neq "" (set %~2=%result%) else echo %result% 

try this.Some special symbols like ! and ^ could cause trouble though. You can also use findstr:

@echo off 

:isIntererFindstr  input [returnVar] 
setlocal enableDelayedexpansion 
set "input=%~1" 

if "!input:~0,1!" equ "-" (
    set "input=!input:~1!"
) else (
    if "!input:~0,1!" equ "+" set "input=!input:~1!"
)

echo !input!|findstr /r "[^0-9]" >nul 2>&1

if %errorlevel% equ 0 ( 
        set result=false 
) else ( 
        set result=true 
)
endlocal & if "%~2" neq "" (set %~2=%result%) else echo %result% 
npocmaka
  • 55,367
  • 18
  • 148
  • 187