Extract number from string in batch file

后端 未结 4 646
余生分开走
余生分开走 2021-01-07 06:54

From a batch file I want to extract the number 653456 from the following string:

C:\\Users\\testing\\AppData\\Local\\Test\\abc123\\643456\\VSALB         


        
相关标签:
4条回答
  • 2021-01-07 07:05
    @echo off 
    setlocal EnableDelayedExpansion
    set "string=C:\Users\testing\AppData\Local\Test\abc123\643456\VSALBT81_COM"
    
    for /L %%d in (0,1,9) do set "string=!string:\%%d=\ %%d!"
    for /F "tokens=2" %%a in ("%string%") do for /F "delims=\" %%b in ("%%a") do echo Number: [%%b]
    
    0 讨论(0)
  • 2021-01-07 07:12

    Assuming the number is always the parent folder (the folder before the end):

    @echo off
    set "str=C:\Users\testing\AppData\Local\Test\abc123\643456\VSALBT81_COM"
    for %%F in ("%str%\..") do set "number=%%~nxF"
    
    0 讨论(0)
  • 2021-01-07 07:18

    EDIT - Code sample adapted to correct errors shown in comments

    set d=C:\Users\testing\AppData\Local\Test\abc123\643456\VSALBT81_COM
    for %%f in ("%d:\=" "%") do for /f %%n in ('echo %%f^|findstr /b /e /r "\"[0-9]*\""') do (
        echo %%~n
    )
    

    Just precede the path with a quote, split the path, replacing each backslash with a quote a space and a quote and append a quote (so we have a list of elements to iterate), and for each part check if it is formed only by numbers

    0 讨论(0)
  • 2021-01-07 07:21

    This uses a helper batch file called repl.bat from - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

    @echo off 
    set "string=C:\Users\testing\AppData\Local\Test\abc123\643456\VSALBT81_COM"
    echo "%string%"|repl ".*\\([0-9]*)\\.*" "$1"
    
    0 讨论(0)
提交回复
热议问题