Replace one identifier with another

Advanced Renamer forum
#1 : 28/03-21 20:15
Posts: 1
I have a collection of ~700 ebooks from one publisher.
All filenames follow a
<Category> - <Title> [<PublisherCode>].<Ext>
I have a CSV file with (among other data), the Publisher codes and matching ISBNs.
I want to replace the publisher codes in each file name with the ISBN.

Is this possible?
If so, can someone offer some guidance on what methods I should be using?

The CSV does not match my collection 1:1.
The CSV has books I do not, and I have books not listed in the CSV.

28/03-21 20:15 - edited 28/03-21 20:18
#2 : 29/03-21 13:43
David Lee
David Lee
Posts: 662
That was fun!

Try this script method...

In the Pre batch script:

replace = {};
csv = "C:\\Users\\profile\\path\\list.csv";
csv = '::\"' +csv + '\"';
while (line = app.parseTags('<File Line:' + j + csv + '>')) {
match = line.match(/(.*),(.*)/);
replace[match[1]] = match[2];

and in the main script window:

match =*\[)(.*)(\].*)/);
isbn = replace[match[2]];
if (isbn) return match[1] + isbn + match[3];

The full path to your CSV file is saved in the variable "csv" in the Pre batch file - you must edit this to point to the location of your own file. Note that you must replace all instances of "\" in the path with "\\".

The Pre batch script scans through your CSV file using the <File Line> tag and saves the Publisher codes and ISBN into properties of the object "replace" where the codes define its property names and the ISBN the corresponding values.

Note that the Regular Expression in line 6 of the Pre batch script assumes only two columns in the CSV file, with codes in column 1 and ISBN is column 2 and with comma as the separator. If the structure of your file differs then you will have to edit the regex to suit. Also the code assumes that there is no header line in the CSV file - if you have a header line then start at line 2 by initializing j=2 before the While loop.

The main script then extracts the publisher code from each filename (using "[" & "]" as delimiters) and tries to fetch the corresponding ISBN from the "replace" object. If property is absent then the value "undefined" is returned, otherwise it is applied to the replacement filename.

Note that the <File Line> tag is not described in the User Guide. However you will find it in the entry for Version 3.61 (14. jan. 2014) on the "What's New" page:

29/03-21 13:43