I would like to extract first number, found in a path string.
Some examples
c:\dir\release1\temp should extract: 1
c:\dir\release11\temp should extract: 11
c:\dir\release1\temp\rel2 should extract: 1
c:\dir\release15a\temp should extract: 15
My current code, that loops trough folder names, and tests if folder name is a number (I need some changes here):
setlocal enableextensions enabledelayedexpansion
set line=one\two\three\4\pet\0\sest\rel6\a
rem set line=%cd%
:processToken
for /f "tokens=1* delims=\" %%a in ("%line%") do (
echo Token: %%a
set line=%%b
rem need fix here: need to extract number from string
echo %%a|findstr /r /c:"^[0-9][0-9]*$" >nul
if errorlevel 1 (echo not a number) else (echo number)
)
if not "%line%" == "" goto :processToken
endlocal
Thanks!
EDIT:
I wanted to parse number from that path string.
Well I’ve found solution that checks only last 3 characters of string. It’s ok for now.
::test last 3 characters
set relno=!token:~-3,3!
echo !token:~-3,3!|findstr /r /c:"^[0-9]*$" >nul
if errorlevel 1 (echo not number) else (echo number)
::test last 2 characters
set relno=!token:~-2,2!
echo !token:~-2,2!|findstr /r /c:"^[0-9]*$" >nul
if errorlevel 1 (echo not number) else (echo number)
::test last character
set relno=!token:~-1,1!
echo !token:~-1,1!|findstr /r /c:"^[0-9]*$" >nul
if errorlevel 1 (echo not number) else (echo number)
Okay, here’s a batch only version. It implements isdigit, walks the input looking for the first digit, and stops and prints the characters between when it hits the end or a non-digit.
This is slow – the longer the input, the slower it is.
Sample output: