Batch DOS - Replace "part of" filenames

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
embassy81
Posts: 2
Joined: 29 Nov 2019 09:57

Batch DOS - Replace "part of" filenames

#1 Post by embassy81 » 29 Nov 2019 10:03

Hi guys, sorry me if there is something similar, but i cant find something to help me.
i have some file named:
11223344_0123_.pdf
11223355_1234_.pdf
(or if can be more simply i can generate name as 11223344_0123.pdf, whitout last underscore)

i need rename all files without chars beetween underscore, and obtain:
11223344.pdf
11223355.pdf

i have write this

Code: Select all

setlocal enabledelayedexpansion
for %%a in (*_*) do (
  set file=%%a
  ren "!file!" "!file:_=!"
)
but i cant find something to say "from first _ to last _
similar

Code: Select all

  ren "!file!" "!file:_*_=!"
but in this way no work

can you help me? really thanks
Last edited by aGerman on 29 Nov 2019 10:38, edited 1 time in total.
Reason: code formatting

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch DOS - Replace "part of" filenames

#2 Post by aGerman » 29 Nov 2019 10:43

You should use a FOR /F loop with underscore as delimiter.
Untested:

Code: Select all

for %%a in (*_.pdf) do for /f "delims=_" %%b in ("%%~na") do (
  ren "%%~a" "%%b%%~xa"
)
Steffen

embassy81
Posts: 2
Joined: 29 Nov 2019 09:57

Re: Batch DOS - Replace "part of" filenames

#3 Post by embassy81 » 03 Dec 2019 09:06

Really thx!

at the end i use this method

Code: Select all

for %%a in (*_*) do (
  set file_completo=%%a
  set file_nome=%%~na
  set name=!file_nome:~0,-6!
  ren "!file_completo!" "!name!.pdf"
)
Last edited by dbenham on 03 Dec 2019 09:23, edited 1 time in total.
Reason: add code tag

Post Reply