Search code examples
regexbatch-file

Extract number from string in batch file


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

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

The number will change, however it will always be just digits.

My current theory is to search for something that fits \alldigits\, then replace the two \s with white space, but I can’t quite get it.


Solution

  • 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"