JSI Tip 10600. How can I extract the numeric characters from a string?

I have scripted OnlyNumeric.bat to extract the numeric characters from a string.

The syntax for using OnlyNumeric.bat is:

call OnlyNumeric String NumericVariableName


String              is the string that you wish to parse.

NumericVariableName is a call directed environment variable that will contain the numeric characters in String.


If the variable telephoneNumber contained 1 (800) 555-1212, then:

call OnlyNumeric %telephoneNumber% NumTelNo

would set the NumTelNo variable to 18005551212.

NOTE: If String contains no numeric characters, then NumericVariableName will be undefined.

NOTE: See How can I update all users telephone number to contain only numeric characters?

OnlyNumeric.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax: OnlyNumeric String NumericVariableName&goto :EOF
if exist "%TEMP%\OnlyNumeric.vbs" goto doit
@echo.dim str, newstr, num, c1, c2>"%TEMP%\OnlyNumeric.vbs"
@echo.Set objshell = CreateObject("WScript.Shell")>>"%TEMP%\OnlyNumeric.vbs"
@echo.Set objArgs = WScript.Arguments>>"%TEMP%\OnlyNumeric.vbs"
@echo.num = Len(str)>>"%TEMP%\OnlyNumeric.vbs"
@echo.For I = 1 To num>>"%TEMP%\OnlyNumeric.vbs"
@echo. c1 = Mid(str, I, 1)>>"%TEMP%\OnlyNumeric.vbs"
@echo. if c1 ^>= "0" and c1 ^>"%TEMP%\OnlyNumeric.vbs"
@echo.  c2 = c2 ^& c1>>"%TEMP%\OnlyNumeric.vbs"
@echo. end if>>"%TEMP%\OnlyNumeric.vbs"
@echo.Wscript.Echo c2>>"%TEMP%\OnlyNumeric.vbs"
set %2=
for /f "Tokens=*" %%a in ('cscript.exe //nologo "%TEMP%\OnlyNumeric.vbs" %1') do (
 set %2=%%a

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.