0

So the problem is that I will get filename such as:

"a.b.c.d.e.f.g"

and I need to get the 2 last portions. In this case,

"f.g"

and I don't know how many dots the string will have.

Thx for your help!

Franko
  • 131
  • 1
  • 10

3 Answers3

3
@ECHO OFF
SETLOCAL
SET "fname=a.b.c.d.e.f.g"

FOR %%a IN (%fname%) DO FOR %%b IN (%%~na) DO ECHO %%~xb%%~xa
Magoo
  • 77,302
  • 8
  • 62
  • 84
2

try this:

@echo off &SETLOCAL
SET "fname=a.b.c.d.e.f.g"
:loop
SET /a t+=1
SET "d="
FOR /f "tokens=%t% delims=." %%a IN ("%fname%") DO SET "d=%%a"
SET "f=%g%"&SET "g=%e%"&SET "e=%d%"
IF DEFINED d GOTO :loop
ECHO %f%.%g%

€: works for all delimiters, not only ..

Endoro
  • 37,015
  • 8
  • 50
  • 63
1

You could use the parameter modifier x in %~xI` to access only the extension and then use it twice.

@echo off
set "fname=a.b.c.d.e.f.g"
for /F "delims=" %%A in ("%fname%") DO (
   set "ext2=%%~xA"
   for /F "delims=" %%A in ("%%~nA") DO set "ext1=%%~xA"
)
echo %ext1%%ext2%  
jeb
  • 78,592
  • 17
  • 171
  • 225