Rename a file with name of another file without using folder name

Advanced Renamer forum
#1 : 04/02-20 15:36
Larry
Posts: 3
I have thousands of scanned photos that are paired and sometimes tripled, not with the same file name and different extensions (like IMG_0123.jpg and IMG_0123.raw ) but, rather, with slightly different file names and the same extension to differentiate an "original" photo from one and sometimes two different color-corrected ones. Each file pair (or, sometimes, triple) is named using the following convention: (1) the original file is FF_#### [description].jpg (#### being a 4-digit number sequence), (2) FF_####a.jpg (same number sequence with suffix "a" to denote the first color corrected image file), and (3) FF_####b (same number sequence, but with "b" suffix to denote the second color-corrected image (if that exists). An actual example is: (1) FF_2059 [Marc 1971-10-02].jpg, (2) FF_2059a.jpg, and (if there is a third) (3) FF_2059b.jpg. There is always at least one "a" color-corrected file and sometimes, but not always a "b" file as well.

What I want to do is to rename the "a" and "b" files to have the same bracketed description as the original file, but keeping the "a" and "b" suffixes. I have many thousands of these pairs and triples in a folder, and have tried everything I can think of but cannot figure out a way to use the existing renaming methods to transfer the description of the base file name to the "a" and "b" files. I tried embedding the file name of the base file into the file's metadata (e.g., putting "FF_2059 [Marc 1971-10-02]" into the metadata title field), and then using the New Name method with <ExifTool:Title> as the new name and Pair Renaming. But that doesn't work because, apparently, <ExifTool:Title> looks for the title in the paired "a" file's metadata, finds nothing, so erases the name of the "a" file rather than take the name of the base file and use it for the paired "a" file name.

I have looked through the forum and, unless I missed it, do not see a solution to my problem. I sense that if what I want to do can be done by AR, it'll require a custom Script. But I don't program, so I'm stuck. Can you help? Thanks.
#2 : 04/02-20 21:25
David Lee
Posts: 1125
If you drag your folder into the list and select "Add the files in the folders" the filenames should be sorted into ascending alphanumerical order and, since "space" comes before "a" & "b", the original files should precede their respective colour corrected images.

Then all you have to do is compare each filename with the preceding one and add the description if the file prefixes match.

First of all create a couple of variables in the Pre batch script to keep track of the previous original file scanned and set these to null strings:

var oldPrefix ="";
var oldSuffix = "";

Then in the main script window enter the script:

name = item.name.match(/(\FF_\d{4})(.*)/);
prefix = (name[1]);
suffix = (name[2]);
if (prefix.localeCompare(oldPrefix)) {
oldPrefix = prefix;
oldSuffix = suffix;
} else {
return prefix + oldSuffix + suffix;
}

The localeCompare method will return a non-zero value (interpreted as "True" in the if statement) if the latest prefix is different from oldPrefix, in which case the values of oldPrefix and oldSuffix are updated and nothing is returned.
Otherwise the oldSuffix (" [description]" ) is inserted between the current prefix ("FF_dddd") and suffix ("a" or "b") and the resulting string is returned as the new filename.

The script will accept any number of colour-corrected files (or zero).

I haven't coded any error checking so the script assumes that there are no rogue filenames in the list and that every colour-corrected file has an equivalent original image.

However, if there are other files in the directory then you can exclude them from the list by using the Regular expression match \FF_\d{4}( \[.*]|a|b)\. in the "Add folder" dialogue.

edited: 04/02-20 21:38
#3 : 05/02-20 23:10
Larry
Posts: 3
Reply to #2:

THANK YOU!!!! This is fantastic. The code does exactly what I needed done.

Again, thank you very much. Advanced Renamer is outstanding, and your gracious support and assistance just makes it more so.