Regular Expressions (Date Matching and Conversion) – Perl 5 and Bulk Rename Utility.
I occasionally find myself modifying my digital photo naming convention, and subsequently end up having to rename a lot of photos (thousands!). The best program that I have found for renaming is called Bulk Rename Utility (BRU). BRU is lightweight, powerful, and easy to use once you get the hang of PCRE regular expressions.
Bulk Rename Utility utilizes/supports the Perl 5 regular expression conventions. In other words, any syntax that works in Perl 5 should also work in BRU.
Using PCRE Regex Patterns in Bulk Rename Utility
If you wanted to convert a date in a filename with a 4-digit year to one with a 2-digit year for some reason, you could start with this Perl-compatible regular expression (PCRE) to detect and “capture” the parts of your date:
The parentheses are called regex capture groups (or capturing groups). The above regex contains four capture groups. The first group is this one “(19|20)”, which captures dates that start with 19 or 20 (1984, 1903, 2011, 2033, etc.) But, since we are not interested in keeping that part of the year (just the last two digits) and parentheses aren’t required for logical OR to work, you might think you can omit them. However, you would be wrong. I’m not exactly sure what the root cause is, but through my own testing, I have found that the regex will break if you don’t include parenthesis around a logical OR statement. This is a gotcha.
Next, you need to build the renaming portion of your regex for a process called substitution. Here is the code for rebuilding dates in the format “mm-dd-yy”:
The results of our captures are held in “slash variables” (\1 = element 1, \2 = element 2, etc.). I’m not sure if that’s an established term, so I’ll just coin it now.
Notice, since we have 4 capture groups we ignore the first one, as already discussed. The second group “\2” is holding our 2-digit year, the third our 2-digit month, and the fourth our 2-digit day.
Here is the final replacement portion which will append the string “photo_” to the front of each renamed file name:
That’s pretty much it.
Enter the first part in the “Match” field of the RegEx module, and the second part in the “Replace” field. In the file list “New Name” column you wil see a preview of what each new filename is going to be.
As in many programs and apps you can use SHIFT-CLICK to select all and CONTROL-CLICK to select multiple items in your file list. If a preview is in red then there is an error with your regex and the file name will not be changed. If the preview remains black then it doesn’t match the regex and also won’t be changed. But, if the preview is green, then your original filename matches the regex and will be changed. Click the “Rename” button in the lower right corner when you are ready.
Social Engagement Questions
So, now it is your turn. Here are some questions to make you think and inspire engagement. We want to hear from you!
- What bulk file renaming software do you use?
- How experienced are you with regex?
- What are some other digital file system organization tips that make your life easier?