Extract numbers only from a string - optimization

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 555
Joined: 28 Jun 2010 03:46

Extract numbers only from a string - optimization

#1 Post by miskox » 05 Jul 2012 00:10

Hi all,

I have this simple code (it works, but it is slow):

Code: Select all

@echo off
set mystring=x56x723@c8
echo !%mystring%!
call :NUMBERS_ONLY "%mystring%" mystring
echo !%mystring%!

set mystring=x2c3c4c5v6
echo !%mystring%!
call :NUMBERS_ONLY "%mystring%" mystring
echo !%mystring%!

goto :EOF






:NUMBERS_ONLY
set _source=%~1
set _out=%2
set /a _cnt=0
set newstring=&rem
:00
call set tmpstring=%%_source:~%_cnt%,1%%
if "%tmpstring%"=="" set %_out%=%newstring%&&goto :EOF
if "%tmpstring%"=="0" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="1" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="2" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="3" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="4" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="5" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="6" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="7" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="8" set newstring=%newstring%%tmpstring%
if "%tmpstring%"=="9" set newstring=%newstring%%tmpstring%
set /a _cnt=_cnt + 1
goto :00


Output:

Code: Select all

!x56x723@c8!
!567238!
!x2c3c4c5v6!
!23456!


Is it possible to optimize it?

Thanks,
Saso

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Extract numbers only from a string - optimization

#2 Post by foxidrive » 05 Jul 2012 00:46

miskox wrote:I have this simple code (it works, but it is slow):

Is it possible to optimize it?



Code: Select all

@echo off
set mystring=x56x723@c8
echo %mystring%
for /f "tokens=1-20 delims=abcdefghijklmnopqrstuvwxyz!@#$&*()-=" %%a in ("%mystring%") do echo "%%a%%b%%c%%d%%e%%f%%g%%h%%i%%j%%k%%l%%m%%n%%o"
pause

Post Reply