Replace one identifier with another

Advanced Renamer forum
#1 : 29/11-21 03:08
Susan
Susan
Posts: 6
I'm trying to do something in Advanced Renamer 3.88 very similar to what Joey was trying to do in https://www.advancedrenamer.com/forum_thread?for um_id=11652 , basically replacing one set of prefixes in file names with another.

When I tried the scripts listed by David Lee in his answer to Joey, I got this error message for every file:

Invalid script: Invalid script: uncaught: 'parse error (line 4)'

David's advice includes:

...and in the main script window:

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

I noticed that the main script window puts
function (index, item) {
before any code pasted there, and puts
}
after. What else should I be doing to take this into account?


29/11-21 03:08
#2 : 30/11-21 00:39
David Lee
David Lee
Posts: 1071
Just ignore it and enter your code in the window.

The Script method runs your code as a function and passes the index number and the object defining each item in the list so that you can access them in your code. As far as you are concerned this is just housekeeping and you don't need to worry about it.

My script worked perfectly with test data for the previous application and since the OP didn't do me the courtesy of a reply I can only assume that it must have worked for him as well.

What exactly are you trying to accomplish and what code have you entered? Your error messsage refers to line 4 but my script only has 3 lines in the main program!



30/11-21 00:39 - edited 30/11-21 09:24
#3 : 30/11-21 11:43
Susan
Susan
Posts: 6
Reply to #2:

There's something going wrong somewhere, so here's everything I'm using:

I put this code in the Pre batch script window:

csv = "C:\\Users\\Susan\\Desktop\\Advanced Renamer test\\Prefixlist.csv";
csv = '::\"' +csv + '\"';
j=1;
while (line = app.parseTags('<File Line:' + j + csv + '>')) {
match = line.match(/(.*),(.*)/);
replace[match[1]] = match[2];
j++;

and this code in the batch Script window:

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

for 3 files to rename:

1F23 - Authorization.txt
3A2 -Discrepancies.txt
4A15 - TIN.txt

and the .csv file "Prefixlist.csv" which is entirely

1F23,2F2
3A2,1B
4A15,5A17

and located in C:\Users\Susan\Desktop\Advanced Renamer test\ .

After I run the scripts, I now get this error message for each of the 3 files:

Invalid script: Invalid post script: uncaught 'cannot read property 2 of null'

What is it trying to read property 2 of, that is null and should be something else? Is anything else wrong here?


30/11-21 11:43
#4 : 30/11-21 11:49
Susan
Susan
Posts: 6
Also, I just now realized something: I'm specifically trying to replace prefixes, not something in the middle of a file name. So, I tried replacing

csv = '::\"' +csv + '\"';

with

csv = '+csv + '\"';

in the Pre batch script, now I get the error message

Invalid script: invalid input pre script: uncaught 'invalid escape (line 3)'

instead.


30/11-21 11:49 - edited 30/11-21 11:50
#5 : 01/12-21 23:51
David Lee
David Lee
Posts: 1071
Reply to #4:

You have two issues...

1) You missed the first and last statements of the Pre batch script:

replace = {};
csv = "C:\\Users\\Susan\\Desktop\\Advanced Renamer test\\Prefixlist.csv";
csv = '::\"' + csv + '\"';
j=1;
while (line = app.parseTags('<File Line:' + j + csv + '>')) {
match = line.match(/(.*),(.*)/);
replace[match[1]] = match[2];
j++;
}

2) It is the regular expression in the Match method in the main script that needs to be modified:

match = item.name.match(/^([^ -]*)(.*)/);
if (prefix = replace[match[1]]) return prefix + match[2];


01/12-21 23:51
#6 : 02/12-21 01:20
Susan
Susan
Posts: 6
Reply to #5:

It works! Thanks a ton David! :D


02/12-21 01:20
#7 : 02/12-21 01:28
Susan
Susan
Posts: 6
Reply to #5:

Also, one more question if you don't mind:

I know the prefix 1F2,3 (in the actual files I need to convert) would be a problem compared to 1F23 (in the sample I made) because of the .csv file.

Is there a way I can use a spreadsheet that isn't separated by commas for this, so I wouldn't even need to take the comma out of 1F2,3 each time first?


02/12-21 01:28
#8 : 02/12-21 09:06
David Lee
David Lee
Posts: 1071
Reply to #7:
No problem!

The Pre batch script extracts the old and new prefixes from each line of the "csv" file using the match method: match = line.match(/(.*),(.*)/);

The pattern between the "/" characters is a regular expression.

".*" will match any string of characters (including a null string) in the string variable "line" and enclosing it in parenthesis will save the result of each match in an element of the array variable "match". The comma simply represents itself.

Thus line.match(/(.*),(.*)/) looks for two strings separated by a comma and returns the two strings as elements of the array "match".

So if line = 4A15, 5A17

The result will be :
match[1] = 4A15
match[2] = 5A17

The comma is simply a separator so you can replace it with anything else that does not appear in any of the prefixes.

eg if you separate the old and new prefixes with semi-colons: eg "4A15; 5A17"
then just change line 6 of the Pre batch script to:

match = line.match(/(.*);(.*)/);

For a basic introduction to regular expressions see https://www.advancedrenamer.com/user_guide/regul ar_expresions

Although be aware that the usage is a bit different in JavaScript than in "normal" renaming methods.


02/12-21 09:06
#9 : 02/12-21 11:01
Susan
Susan
Posts: 6
Reply to #8:

It works again, this is great, thanks!


02/12-21 11:01