Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
You can use what are called "regular expressions" to deal with complex patterns. Google the term for tutorials and sample code.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
The previous regex presumed a pattern of the first character being an uppercase letter, followed by any combination of letters or numbers...
This may (or may not be) be more robust:
(.+)([A-Z].+\.[^\.]+$)
Again we need samples, samples, samples...
Copy link to clipboard
Copied
This is all very useful, thank you!
I don't have any patterns unfortunately, the only pattern is the repetition.
Sometimes the title starts with an uppercase, some others with lower case. Some contain spaces and some underscores. I feel this is very complicated.
I'll try and see if I can run multiple batch rename without making it any worse.
Best
Copy link to clipboard
Copied
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.