Q. How do I determine if a string is all alphabetic or all numeric?
February 3, 2003
While processing strings in a batch script, you may need to know if the string contains only alphabetic (A-Z / a-z) or only numeric (0-9) characters.
I have scripted Alphabetic.bat and Numeric.bat. The syntax for using these tools are:
call alphabetic string answer
or
call numeric string answer
where string is the environment variable you wish to interrogate and answer is a call directed environment variable that will be set to a Y or a N.
Alphabetic.bat contains:
@echo off
setlocal
set string=%1
set string=%string:"=%
if {%2}
{} @echo Syntax: Alphabetic string answer&endlocal&goto :EOF
@echo %string%|findStr /i "[^a-z]">nul
endlocal
set %2=N
if %ERRORLEVEL% EQU 1 set %2=YNumeric.bat contains:@echo off
setlocal
set string=%1
set string=%string:"=%
if {%2}
{} @echo Syntax: Numeric string answer&endlocal&&goto :EOF
@echo %string%|findStr "[^0-9]">nul
endlocal
set %2=N
if %ERRORLEVEL% EQU 1 set %2=Y
NOTE: See How do I parse a string using position and length variables?
NOTE: See How do I determine if an all alphabetic string is all upper case or all lower case?
Read more about:
Alphabet Inc.About the Author
You May Also Like