How to rename parts of filenames with certain target patterns?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

How to rename parts of filenames with certain target patterns?

#1 Post by pstein » 27 Dec 2019 01:39

How can I (from cmdline!) rename parts of filename (and leave the remaining part untouched)?

Simplified example:

Assume I have the following files:

test aabb cc 13412542552.pdf
second aabb cc 324236236.pdf
other aabb cc 2345325.pdf

Now I want to replace the pattern " aabb cc " by " (newpattern 2019) " so that the filenames are as follows:

test (newpattern 2019) 13412542552.pdf
second (newpattern 2019) 324236236.pdf
other (newpattern 2019) 2345325.pdf

Possible other files should be renamed accordingly.

Keep in mind that a file like:

norename-aabb cc 356345634.pdf

should NOT be renamed (mind the enclosing blanks!).

How can I achieve this (in general)?
If necessary a (free), simple, third party cmdline tool is acceptable.

No difficult regular expressions are necessary.

Peter

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

Re: How to rename parts of filenames with certain target patterns?

#2 Post by aGerman » 27 Dec 2019 06:23

(from cmdline!)
Then the line below requires to navigate to the target folder beforehand.
Note I suppose "from cmdline" meant to be "not from within a Batch script".
No difficult regular expressions
I use FINDSTR which doesn't support difficult regular expressions anyway. So It's very basic regex.

Code: Select all

for /f "delims=" %i in ('dir /a-d /b *.pdf^|findstr /rixc:"[^ ][^ ]* [^ ][^ ]* [^ ][^ ]* [0-9][0-9]*\.pdf"') do @for /f "tokens=1,4" %j in ("%i") do @ECHO "%i" "%j (newpattern 2019) %k"
This line only displays how the line would rename the files. Replace @ECHO with @REN to perform the renaming action.

Steffen

Post Reply