extract second last Substring in bat file

Viewed 149

I am trying to get the substring which lies between the last & second last fullstop (.) in a filename using the batch file, filename can have any number of fullstops. The file name will look like as follows, in this particular example output should be '8'.

filename: a_b_c_d_e.1.2.3.4.5.6.7.8.9

I tried the below part, but it always outputs the string after the first fullstop i.e 1.2.3.4.5.6.7.8.9 instead of '8'.

set "string=a_b_c_d_e.1.2.3.4.5.6.7.8.9"

@echo %string:*.=%

pause

Any working suggestions can be helpful.

1 Answers

You could use the file modifier syntax.

Replace all dots with backslashes and then take the parent directory by appending \..

@echo off
set "string=a_b_c_d_e.1.2.3.4.5.6.7.8.9"

for /F "delims=" %%X in ("%string:.=\%\..") do echo %%~nX
Related