• Welcome to the Lightroom Queen Forums! We're a friendly bunch, so please feel free to register and join in the conversation. If you're not familiar with forums, you'll find step by step instructions on how to post your first thread under Help at the bottom of the page. You're also welcome to download our free Lightroom Quick Start eBooks and explore our other FAQ resources.
  • Stop struggling with Lightroom! There's no need to spend hours hunting for the answers to your Lightroom Classic questions. All the information you need is in Adobe Lightroom Classic - The Missing FAQ!

    To help you get started, there's a series of easy tutorials to guide you through a simple workflow. As you grow in confidence, the book switches to a conversational FAQ format, so you can quickly find answers to advanced questions. And better still, the eBooks are updated for every release, so it's always up to date.
  • Dark mode now has a single preference for the whole site! It's a simple toggle switch in the bottom right-hand corner of any page. As it uses a cookie to store your preference, you may need to dismiss the cookie banner before you can see it. Any problems, please let us know!

REGEX Bulk Renaming

Status
Not open for further replies.

Carsten

New Member
Joined
Apr 5, 2019
Messages
9
Lightroom Version Number
Lightroom Classic 12.0.1
Operating System
  1. Windows 10
Hello everybody,

I have the following problem with bulk renaming:

I want all my RAW files in Lightroom to be named <YYYY>-<MM>-<DD>_<Custom Text>-<Number Suffix> with <Number Suffix> being the original filename number suffix assigend by the camera when the image was captured. This can easily be done with the Rename Photo dialog iff the original number suffix is at the very end of the current filename.

Unfortunately, I once named some hundred files like this: <Custom Text>-<Number Suffix>_<YYYY>-<MM>-<DD>. In theses cases Lightroom always takes the <DD> as the number suffix, irrespective of my use of <Filename number suffix> or <Original number suffix>.

It is also not possible to rename the photos to their original filename and the do a proper second rename. Lightroom always takes the current filename as the <Original filename>.

And the renaming also fails for HDR photos taken with Lightroom mobile which are named APC_<Number>-hdr on an iPhone. In these cases, Lightroom does not identify any number suffix at all, it is just empty.

A proper REGEX renaming feature does not seem to be available.

Any helps is really appreciated!
 
It is also not possible to rename the photos to their original filename and the do a proper second rename. Lightroom always takes the current filename as the <Original filename>.
You should be able to rename the "wrong" files back to the original filename, by using the "Preserved Filename" option in the rename dialog (use the "Original Filename" drop-down list to see and select "Preserved Filename"). Once they have been renamed back to the original name, you can rename them again using your current preferred template.
 
You should be able to rename the "wrong" files back to the original filename, by using the "Preserved Filename" option in the rename dialog (use the "Original Filename" drop-down list to see and select "Preserved Filename"). Once they have been renamed back to the original name, you can rename them again using your current preferred template.
Hi Jim,
I had the same idea but it doesn't work too. The changed filename just comes out as "_.NEF". So, the original filename does not seem to be available.
My unfortunate or wrong naming was done upon import of the images, maybe the original (!) filename got lost in this process. I have also just checked the metadata in the NEF-file and the original filename does not seem to be stored there.

But: in the Nikon-section of the metadata in the NEF-file there is a tag called FileNumber, which contains the original <Number Suffix>. Mayby I can somehow bulk copy this to one of the metadata fields available in the Rename Photo dialog and then do the bulk rename.
(In the Lightroom Mobile HDR images there does not seem to be a comparable tag, though.)
 
If you look in the Metadata panel for one of those images, select the "Default" template, is the "Preserved Filename" field populated?
 
If you look in the Metadata panel for one of those images, select the "Default" template, is the "Preserved Filename" field populated?
The fields "File Name", "Original Filename" and "Preserved File Name" in Lightroom's Metadata panel all contain the same, changed filename.
 
I wonder....are the images that you are looking at from earlier than around mid-2019? I know that "Preserved Filename" wasn't introduced until a few years ago, so anything earlier than that wouldn't have had the real "Preserved Filename" stored in the catalog, so it currently shows that as the import filename in my catalog from all images before that time. Since then, all images have the original SOOC filename correctly stored in that "Preserved Filename" field.

Alternatively, were the images imported with the current filename (e.g. from another catalog or other image app)?
 
Unfortunately, I once named some hundred files like this: <Custom Text>-<Number Suffix>_<YYYY>-<MM>-<DD>
I assume from your last post that you did this improper rename with outside of LrC before you imported the images, or your re-imported the images for some reason after using LrC to do the rename. Otherwise the file name as originally shown in the import dialog (prior to LrC doing the rename) would be present in the "preserved file name" metadata field.

So, at this point I would say that there is nothing your OS can see and nothing in the image metadata that contains the original 4 digit numer from the camera. This then would mean that in order to un-do the mistake you could....

1) see if you have backup in your system that has the original file names

2) (what I'd do) Look in your LrC catalog and use filters, sort or smart collection to determine the last image in LrC that has the correct file name format. Let's say its camera supplied image number is 1234. Then take all the images with the incoreect file names and sort them by capture date. Finally use LrC to rename these files using a file name template similar to the one you usually use, but the last token should be "Sequence # (0001)" rather than "Filename number Suffix". Select all the images and rename with this template and in the dialog box set the starting number to one greater than the previous high number from correct files (1235 in my example). This will give you a reasonably close approximation of the original camera number.
 
I want all my RAW files in Lightroom to be named <YYYY>-<MM>-<DD>_<Custom Text>-<Number Suffix> ... Unfortunately, I once named some hundred files like this: <Custom Text>-<Number Suffix>_<YYYY>-<MM>-<DD>.

A couple options:

1. Use the Write Data Field command of the Bag-o-goodies plugin to set Caption, Title, Headline, or some other unused metadata field recognized by LR's renaming to the desired new file name, using this preset token (with all the line breaks removed):
Code:
{LUA=(function ()
    local text, suffix, date =
        LibraryFilename:match ("^([^%-]+)%-(%d+)_(%d+%-%d+%-%d+)$")
    return date .. "_" .. text .. "-" .. suffix
    end) ()}
I've tested the pattern match but not its use with Bag-o-goodies -- there could be minor errors.

Once you have the rewritten filename in a metadata field, use LR's Rename command with a template that uses that field.

2. Use the LR/Transporter plugin to export a CSV file containing the original filenames. Using your favorite text editor having regular expressions or Excel, add a second column with the desired new names. Use LR/Transporter to import that file back into the catalog, setting an unused metadata field to the second column. Then as in solution 1, use LR's rename command with a template that uses that field.

But: in the Nikon-section of the metadata in the NEF-file there is a tag called FileNumber, which contains the original <Number Suffix>. Mayby I can somehow bulk copy this to one of the metadata fields available in the Rename Photo dialog and then do the bulk rename.

You can do that with Exiftool. The details are left as an exercise for the reader. (Unfortuately, it would take me 30 minutes to construct and test the right magic command line.)
 
I wonder....are the images that you are looking at from earlier than around mid-2019? I know that "Preserved Filename" wasn't introduced until a few years ago, so anything earlier than that wouldn't have had the real "Preserved Filename" stored in the catalog, so it currently shows that as the import filename in my catalog from all images before that time. Since then, all images have the original SOOC filename correctly stored in that "Preserved Filename" field.

Alternatively, were the images imported with the current filename (e.g. from another catalog or other image app)?
That's interesting.
My wrongly named images were imported to Lightroom in 2014 via Import, Move and renamed during import from their original filename _DSCnnnn.NEF.
Actually, up to October 2019 all my NEF-files have the wrong, renamed filename as their Preserved File Name, and all NEF-files shot until then have the original filename as their Preserved File Name. This is in line with your observation.
 
I assume from your last post that you did this improper rename with outside of LrC before you imported the images, or your re-imported the images for some reason after using LrC to do the rename. Otherwise the file name as originally shown in the import dialog (prior to LrC doing the rename) would be present in the "preserved file name" metadata field.

So, at this point I would say that there is nothing your OS can see and nothing in the image metadata that contains the original 4 digit numer from the camera. This then would mean that in order to un-do the mistake you could....

1) see if you have backup in your system that has the original file names

2) (what I'd do) Look in your LrC catalog and use filters, sort or smart collection to determine the last image in LrC that has the correct file name format. Let's say its camera supplied image number is 1234. Then take all the images with the incoreect file names and sort them by capture date. Finally use LrC to rename these files using a file name template similar to the one you usually use, but the last token should be "Sequence # (0001)" rather than "Filename number Suffix". Select all the images and rename with this template and in the dialog box set the starting number to one greater than the previous high number from correct files (1235 in my example). This will give you a reasonably close approximation of the original camera number.
Hi Califdan,

No, the images have been properly imported to Lightroom via Import, Move and renamed during the import. And no, only those images imported in November 2019 and later have the proper original filename as their Preserved File Name.

And yes, the original 4 digit number suffix is contained in the FileNumber tag in the NEF-file, but it does not seem to be accessible in Lightroom, at least not in the Rename Foto dialog. So, it might be possible to copy it into another field accessible in Lightroom.

I have not given up yet, but if I do your suggestion can be a good approximation. Thanks!
 
A couple options:

1. Use the Write Data Field command of the Bag-o-goodies plugin to set Caption, Title, Headline, or some other unused metadata field recognized by LR's renaming to the desired new file name, using this preset token (with all the line breaks removed):
Code:
{LUA=(function ()
    local text, suffix, date =
        LibraryFilename:match ("^([^%-]+)%-(%d+)_(%d+%-%d+%-%d+)$")
    return date .. "_" .. text .. "-" .. suffix
    end) ()}
I've tested the pattern match but not its use with Bag-o-goodies -- there could be minor errors.

Once you have the rewritten filename in a metadata field, use LR's Rename command with a template that uses that field.

2. Use the LR/Transporter plugin to export a CSV file containing the original filenames. Using your favorite text editor having regular expressions or Excel, add a second column with the desired new names. Use LR/Transporter to import that file back into the catalog, setting an unused metadata field to the second column. Then as in solution 1, use LR's rename command with a template that uses that field.



You can do that with Exiftool. The details are left as an exercise for the reader. (Unfortuately, it would take me 30 minutes to construct and test the right magic command line.)
Hi John,
Thanks a lot for these hints. I think I will try method 1 first, this sounds very promising.
 
If you're open to doing it outside LrC and then doing a folder syn, this tool MIGHT work?
https://learn.microsoft.com/en-us/windows/powertoys/powerrename
Yes, doing it outside of LrC would be easy. I would use Emacs, for it has a full-fletched REGEX file rename engine. Thanks for the tool hint anyway.
Actually, though, I shy away from doing this. The images to be renamed are spread over dozens of folders and I fear not to be able to resync the catalog with the images then.
 
Thanks John! I saw this article prior to my question in this forum and read it again now.
The Search and Rename plugin does not seem to provide a full-fletched REGEX or pattern matching mechanism (I am not 100% sure though, because I didn't install it and there is no documentation available on its web page). So I went with Bag-o-Goodies.
I have already solved my problem and I will describe it here in the next days.
 
Hi John,
Thanks a lot for these hints. I think I will try method 1 first, this sounds very promising.
Problem solved.

Based on John's option #1 I did the following:

I used the Write Data Field command of Jeffrey Friedl's Bag-o-Goodies plugin to fill the "Job Identifier" metadata field with the result of the following command: {LUA= return load("X:/my-local-path/extract-file-suffix.txt")}

My file extract-file-suffix.txt contains the following LUA code:
Code:
function getSuffix()
    local text, suffix, date =
        LibraryFilename:match ("^([^_]+)_(%d+)_(%d+%-%d+%-%d+)$")
    return suffix
    end

return getSuffix()

Then I used Lightroom's Rename Foto command to construct the filename I wanted, making use of the "Job Identifier" metadata field now filled with the original filename suffix.

This is almost exactly John's proposal, except that I put the original code in a seperate file, that the getSuffix function only returns the suffix instead of the complete new filename and that I constructed the new filename using Rename Foto.

I used this with slight modifications of the pattern string for recovering the original filename suffix of literarily thouthands of wrongly named images. Once you understand the pattern language of LUA's match function this is really easy. It is documented in the LUA Reference Manual, Secton 6.4.1.

My best wishes to John, your help is really appreciated! Without your code I would not have been able to solve this, for I didn't now about LUA and how to make use of it in Lightroom.
 
Status
Not open for further replies.
Back
Top