1.

Solve : Batch scripting detect alpha characters in a variable.?

Answer»

Windows XP H.

Is there a command, or series of commands, which will DETECT alpha characters in a variable which should only contain numerics, without testing each INDIVIDUAL character?

Purpose:
If the user enters "batfile 1p" instead of "batfile 10" I want to detect that an alpha character exists in the variable, without needing to know what that character is, and echo a warning onscreen.

Thanks

I am sure that you can do something with this

C:\Test>set /A value=hallo1
Invalid number. Numeric constants are either decimal (17),
HEXADECIMAL (0x11), or octal (021).

Checking %errorlevel% after shows a value of 9167 - the only thing you have to worry about is if the user inadvertantly enters a valid hex string

Graham

Code: [Select]
Echo %value%|FindStr /R "[^0-9]" > nul
If %ERRORLEVEL% EQU 0 (
set result=not numeric
) Else (
set result=numeric
)


In this line

Code: [Select]Echo %value%|FindStr /R "[^0-9]" > nul
the result of 'echo %value%' is piped ('|') to become the INPUT to FindStr.
The '/R "[^0-9]" part tells FindStr to search for a regular ('/R')
expression, which is '"[^0-9]"', which means 'not the digits 0 through 9'.
The '> nul' part means 'do not display any results on the screen'. If
ERRORLEVEL is 0 upon completion of the line, it means that the variable %value%
contained at least one non-numeric character, i.e., at least one character
not belonging to the set of digits from 0 to 9.

Thank you for the responses and the excellent breakdown of the command line.



Discussion

No Comment Found