Reputation: 37
in windows command line script I have string variable for an example:
AB_CD RR CRR LH 8AH_M22 1.1 2050_gra.pdf
which is a file name, where I need to extract to a different variable string (in this case) 2050 (user entered time).
It is always bounded by at least one space (usually three) on the left side, and always by _gra.pdf on the right side.
It is not always 4 digits long, and someone might put a dot or comma to separate hours and minutes. So it might look also like this:
Basically is anything between _gra.pdf and nearest space on the left side.
Upvotes: 1
Views: 1601
Reputation: 56180
set "x=AB_CD RR CRR LH 8AH_M22 1.1 2050_gra.pdf"
REM get last element:
for %%i in ("%x: =" "%") do set last=%%i
rem split it by `_`:
for /f "Tokens=1 delims=_" %%i in (%last%) do set utim=%%~i
echo %utim%
edit: to meet "extract anything between _M and next space":
set "x=AB_CD RR CRR LH 8AH_M 22 1.1 2050_gra.pdf"
REM delete anything until (including) "_M":
for %%i in ("%x:*_M=%") do set "y=%%i"
REM get string until next space:
for /f "Tokens=1" %%i in (%y%) do set "utim=%%~i"
echo %utim%
Upvotes: 4