Skip to main content
Participant
February 17, 2021
Question

Batch rename - substitute repetitions

  • February 17, 2021
  • 3 replies
  • 499 views

Hi all,

I have run a batch rename that has gone wrong and can't undo. Now my files contain repetitions: Asia.jpgAsia.jpgAsia.jpgAsia.jpg

 

Is there a way to use String Substitution to identify and remove repetitions?

Many thanks

This topic has been closed for replies.

3 replies

Stephen Marsh
Community Expert
Community Expert
February 18, 2021

To try stop rename problems, I'd suggest that you play things safe in the future:

 

* Manually backup the originals first, or use the Batch Rename feature to copy the files to another directory

 

* Embed the original filenames in the XMP metadata so that they can be recovered if things go wrong (there is a preset to restore the preserved filename, the key is to preserve it first)

 

* Use the preview button to open up the window to double check all files, don't trust the quick preview at the bottom 100%

 

It is quicker to restore/recover backups than to try to rename a rename that had issues.

 

Stephen Marsh
Community Expert
Community Expert
February 17, 2021

Yes, this sort of pattern matching is perfect for a regular expression based string substitution.

 

One of many possible regular expressions would be:

 

Find:

 

(\w+\.jpg)(.+)

 

 

Replace:

 

$1

 

 

If you had a mixture of file extensions (.jpg .tif .psd etc), then different code would be used rather than just the .jpg extension.

 

Some other valid regex find/replace combos include:

 

 

(\w+\.jpg|\w+\.psd)(.+)
$1

(.+)([A-Z][a-z0-9]*?\.\w*?$)
$2

(.+)([A-Z][a-z0-9]*?\.[^\.]+$)
$2

 

 

You can use one of the many sites such as regex101.com to explore the expressions as there are other valid choices, some more elegant than others, some more robust, some may be equivalent etc.

m_augelliAuthor
Participant
February 17, 2021

Thank you for your reply, I'm trying to get my head around regex!

My example was actually wrong, what I have at the moment is:

 

AsiaAsiaAsiaAsia.jpg 

 

that should become

 

Asia.jpg

 

Can you recommend a solution to this?

Best

Stephen Marsh
Community Expert
Community Expert
February 17, 2021

Having multiple examples would help as the pattern may not be consistent.

 

For example, if I presumed that the pattern:

 

AsiaAsiaAsiaAsia.jpg

 

Was always the first 4 characters from the left, or the last 4 characters from the right after the file extention... But that would not help for EuropeEuropeEuropeEurope.jpg

 

If your pattern is an uppercase letter followed by varying lengths of lowercase letters, then one of my previous suggestions works:

 

Find:

(.+)([A-Z][a-z0-9]*?\.[^\.]+$)

 

Replace:

$2

 

 

Legend
February 17, 2021

You can use what are called "regular expressions" to deal with complex patterns. Google the term for tutorials and sample code.