1

I have a bunch of files that I need to rename. They are variable length. Like this:

A1B2C3D4.en.fr.pdf
A1B2C3D4S8.it.fr.pdf
A1B2C3.de.fr.pdf
A1B2C3D4E5.zn.fr.pdf

I want to change them so that I can run a .bat file to make 2 changes: prefix them all with a static prefix, XYZ10; replace the .*.fr.pdf variable ending with the static FRFR.pdf;. So they'll look like this:

XYZ10A1B2C3D4FRFR.pdf
XYZ10A1B2C3D4S8FR.pdf
XYZ10A1B2C3FRFR.pdf
XYZ10A1B2C3D4E5FRFR.pdf

I've been doing it in individual steps each time with power shell but it's a pain to keep doing it and sometimes it does it improperly.

I've tried this:

@echo off
ren *.??.fr.pdf *.FRFR.pdf

but it just makes them look like this:

A1B2C3D4E5.zn.fr.FRFR.pdf

I don't know where to begin with the prefix, I don't really understand any of the things I've been reading about it...

EDIT: This is what I've been doing to prefix in PowerShell.

Dir *.pdf | rename-item -newname {"XYZ10"+ $_.Name}

lycalys
  • 11
  • 2
  • 1
    `for /F "tokens=1* delims=." %%i in ('dir /b "*.??.fr.pdf"') do ren "%%~i.%%~j" "%%~iFRFR%%~xj"` – Gerhard Nov 30 '21 at 17:55
  • 1
    I would use a similar, more complete, and slightly more robust one liner, ```@For /F "EOL=? Delims=" %%G In ('"(Set PATHEXT=) & %SystemRoot%\System32\where.exe ".":"*.??.fr.pdf" 2>NUL"') Do @For %%H In ("%%~nG") Do @For %%I In ("%%~nH") Do @Ren "%%G" "XYZ10%%~nIFRFR%%~xG"``` – Compo Nov 30 '21 at 18:16
  • Thanks, it works. I'll look up the terms used in this so that I can understand how it works. – lycalys Nov 30 '21 at 18:19

1 Answers1

0

There is no simple ren command line to rename as you desire (walk through the thorough post How does the Windows RENAME command interpret wildcards?). I would do it the following way:

rem // Loop through all relevant files:
for /F "delims= eol=|" %%K in ('dir /B /A:-D-H-S "*.??.fr.pdf"') do (
    rem // `%%K` is the full file name, `%%~nK` has got `.pdf` removed, `%%~xK` is `.pdf`.
    for %%J in ("%%~nK") do for %%I in ("%%~nJ") do (
        rem // `%%~nJ` has got `.fr.pdf` removed, `%%~nI` has got `.??.fr.pdf` removed.
        rem // Actually rename the file (complaints in case of conflicts):
        ren "%%K" "XYZ10%%~nIFRFR%%~xK"
    )
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99