Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6007659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:42:44+00:00 2026-05-23T01:42:44+00:00

I would like to extract first number, found in a path string. Some examples

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:42:44+00:00Added an answer on May 23, 2026 at 1:42 am

    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.

    @setlocal
    @echo off
    rem extractfirstnumber.bat
    rem Given a string possibly containing a number, print the first integer.
    rem 123test456 -> 123
    rem Note that special characters may not be properly handled.  (e.g. , ;)
    rem http://stackoverflow.com/questions/6120623/how-to-extract-number-from-string-in-batch
    set input=%1
    if ("%input%") == ("") goto :eof
    call :firstnum input output
    if not ("%output%") == ("") echo %output%&&goto end_success
    goto end_fail
    
    :end_success
    endlocal
    @exit /b 0
    
    :end_fail
    endlocal
    @exit /b 1
    
    :firstnum
    SETLOCAL ENABLEDELAYEDEXPANSION
    call set "string=%%%~1%%"
    set /a index = 0
    set return_number=
    goto firstnum_loop
    
    :firstnum_loop
    if ("!string:~%index%,1!") == ("") goto firstnum_end
    set test_char=!string:~%index%,1!
    call :isdigit test_char is_digit
    rem Found a digit?  Add it to the return.
    if ("%is_digit%") == ("true") set return_number=%return_number%%test_char%
    rem Found a not-digit?  If we found a digit before, end.
    if ("%is_digit%") == ("false") if not ("%return_number%") == ("") goto firstnum_end
    set /a index = %index% + 1
    goto firstnum_loop
    
    :firstnum_end
    ( ENDLOCAL & REM RETURN VALUES
        IF "%~2" NEQ "" SET "%~2=%return_number%"
    )
    goto :eof
    
    :isdigit
    SETLOCAL ENABLEDELAYEDEXPANSION
    set NUMBERS=1234567890
    set found_number=false
    call set "string=%%%~1%%"
    REM If the passed string does not have a single character, return immediately with false.
    if ("%string:~0,1%") == ("") goto isdigit_end
    if not ("%string:~1,1%") == ("") goto isdigit_end
    set /a index=0
    goto isdigit_loop
    
    :isdigit_loop
    if ("!NUMBERS:~%index%,1!") == ("") goto isdigit_end
    set test_char=!NUMBERS:~%index%,1!
    if ("%test_char%") == ("%string%") set found_number=true&&goto isdigit_end
    set /a index = %index% + 1
    goto isdigit_loop
    
    :isdigit_end
    ( ENDLOCAL & REM RETURN VALUES
        IF "%~2" NEQ "" SET "%~2=%found_number%"
    )
    goto :eof
    

    Sample output:

    C:\>extractfirstnumber c:\dir\release1\temp
    1
    C:\>extractfirstnumber c:\dir\release11\temp
    11
    C:\>extractfirstnumber c:\dir\release1\temp\rel2
    1
    C:\>extractfirstnumber c:\dir\release15a\temp
    15
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to test a string containing a path to a file for
I have a string like first url, second url, third url and would like
I would like to extract some lines from a text file, I have started
I would like to extract the date a jpg file was created. Java has
I would like to extract from a general HTML page, all the text (displayed
I have the following snippet where I would like to extract code between the
I have a full MS SQL Backup file that I would like to extract
I would like to know if it is possible, To extract the userid from
In Nant, I would like to be able to extract the last name of
My XML looks like: <key>A</key> How do I extract the string A from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.