JavaScript

Advanced Renamer forum
#1 : 09/04-24 10:59
Andrey
Andrey
Posts: 5
Hello everyone.
There is a large list of files that need to be replaced with a different name.
For example:
097845 - name1
IV-nobux - name2
No3bolt - name3
The list is long.
Option 1. Use list replace, but it is inconvenient to sort in it and there is no way to avoid a duplicate.
Option 2. A file with a list.
There is a list.csv file on the computer (located c:\list.csv). I create the file in Excel, where the first column lists the names to be found (097845, IV-nobux,
No3bolt), and in the second what to replace them with (name1, name2, name3) and save the file in csv format.
Can I write JavaScript so that I take a list of substitutions from list.csv every time ???
Thank you.


09/04-24 10:59
#2 : 10/04-24 01:02
Delta Foxtrot
Delta Foxtrot
Posts: 106
Reply to #1:

Hello Andrey,

As is often the case, this question has been answered on this forum in the past. Here you go, the first one is free! :)

https://www.advancedrenamer.com/forum_thread?for um_id=13535

See if that gets you where you need to go. If you can't work it out from there, post again. We'll work from there.

Best regards,
DF

EDIT: Ok, I feel sort of bad for beating around the bush. You don't really need a script to do what you want. Look in the user manual at the LIST (not list replace) method. Load your target files, add a LIST method, click the "Load list" button and load your .csv, and you are on the road to salvation. If you're worried about duplicate names use a collision rule, like adding a number on the end.
https://drive.google.com/file/d/1ptyiler6jTieg0a 3VLC7aeZIXAixahXc/view?usp=drive_link

I feel better! :)


10/04-24 01:02 - edited 10/04-24 03:55
#3 : 10/04-24 08:10
Delta Foxtrot
Delta Foxtrot
Posts: 106
Reply to #2:

And now I feel terrible. That didn't work, as you've probably noticed. There are two ways to fix it though:
1) Add another method (Replace) that does the following:
Replace: ^[^,]+,
Replace with: <nothing>
Regex: YES
Apply to: Name

Or 2) Use a script method:

PREBATCH:

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

SCRIPT:

if (title = list[item.name]) return title;
------------------
Well, you can't win them all! But it's the final score that counts, right? Right? :)

Best,
DF


10/04-24 08:10 - edited 10/04-24 08:11
#4 : 14/04-24 13:46
Andrey
Andrey
Posts: 5
Reply to #3:
Guys, it doesn't work for me. I'm probably doing something wrong. The file is located on C:\List.scv

https://disk.yandex.ru/i/RdXGuhbzsIT9Fw

I have written a script, but nothing changes.

https://disk.yandex.ru/i/GuVNCBbpEy4HwQ


14/04-24 13:46
#5 : 15/04-24 01:27
Delta Foxtrot
Delta Foxtrot
Posts: 106
Reply to #4:

Hi again Andrey,

No, you didn't do anything wrong–well, except you didn't really make clear what the situation is. Or maybe I didn't read carefully enough, I don't know. That script works only if your filename consists COMPLETELY of the phrase you are replacing. This script ought to do what you really need, to check the filename for the each phrase1 in the csv and replace it in the file in its place with phrase2. Check the .csv filename in the prebatch, but I think it's ok already.
------------------------------------------------------------------------
EDIT: BE SURE that the names in your .csv file don't have quotation marks or anything else around them. I'm not sure what happens if one of them is just a number, because I don't have Excel, but I know that quotes screw up the script. END EDIT
------------------------------------------------------------------------

PREBATCH:

csv = "C:\\list.csv";
csv = '::\"' +csv + '\"';
const oldPart = {};
const newPart = {};
j=0;
while (line = app.parseTags('<File Line:' + (j+1).toString() + csv + '>')) {
match = line.match(/(.*),(.*)/);
oldPart[j] = match[1];
newPart[j] = match[2];
j++;
}

----------------
SCRIPT:

newfilename = item.newBasename ;
var i
for ( i = 0 ; i < app.itemCount; i++) {
if (newfilename.includes(oldPart)) {
newfilename = newfilename.replace( oldPart, newPart );
}
}
return newfilename.trim();

----------------
https://drive.google.com/file/d/1BwwYo3QrSR4iEm3 bTtp4T_Yz7-5YSHRc/view?usp=drive_link

Good luck buddy. I've got my fingers crossed! :)

Best,
DF


15/04-24 01:27 - edited 15/04-24 02:22
#6 : 15/04-24 23:16
Andrey
Andrey
Posts: 5
Reply to #5:
Thank you for trying, showing me how to do it... But I can't do it, the script doesn't work. I'm obviously doing something wrong, but I can't figure out what. :-(

https://disk.yandex.ru/i/_VUDGC19ImqmCg


15/04-24 23:16
#7 : 15/04-24 23:46
Delta Foxtrot
Delta Foxtrot
Posts: 106
Reply to #6:

Andrey,

I am sorry, my friend. It looks like you are doing it all right. And you don't get any error messages, it just doesn't change anything? So weird.

If you want to pursue this, I'm going to set up a debugging script–it's just the same code, with instructions to log information to the console. When I get it posted, if you're still interested, you'll just need to run the program, then open the "Tools/JS Console" and post a screenshot of the results.

I'll be back shortly, don't give up! :)

Best,
DF


15/04-24 23:46
#8 : 16/04-24 01:23
Delta Foxtrot
Delta Foxtrot
Posts: 106
Reply to #7:

Well, I've been going over your screenshot and I can't find anything wrong with the setup.

Here's a debug script. You'll need to set the script method you have to NOT active, add a new script method, and copy these parts into the right places, prebatch and batch. Use a short csv file and only a few filenames in the files list, or the console output will be too big. That setup you showed in the last screenshot should be fine. When you load the files the program will create previews, and in doing that it will log debug information to the JS console. Just click "Tools/JS Console" and a window will open with a lot of stuff. Make a screenshot of that and post it; hopefully we can figure out where the problem is.

PREBATCH:

csv = "C:\\list.csv";
csv = '::\"' +csv + '\"';
app.log("csv = [" + csv + "] at start of prebatch");
const oldPart = {};
const newPart = {};
j=0;
while (line = app.parseTags('<File Line:' + (j+1).toString() + csv + '>')) {
match = line.match(/(.*),(.*)/);
oldPart[j] = match[1];
newPart[j] = match[2];
// debug
app.log("j = " + j.toString());
app.log("line = app.parseTags('<File Line:' + (j+1).toString() + csv + '>') = [" + line + "]");
app.log("match = line.match(/(.*),(.*)/) = [" + match + "]");
app.log("oldPart[j] = [" + oldPart[j] + "]");
app.log("match[1] = [" + match[1] + "]");
app.log("newPart[j] = [" + newPart[j] + "]");
app.log("match[2] = [" + match[2] + "]");
// endDebug
j++;
}
app.log("At end of prebatch.");

SCRIPT:

newfilename = item.newBasename ;
app.log("after line 1 of script; newfilename = [" + newfilename + "]; item.newBasename = [" + item.newBasename + "]" );
var i
for ( i = 0 ; i < app.itemCount; i++) {
if (newfilename.includes(oldPart)) {
newfilename = newfilename.replace( oldPart, newPart );
//debug
app.log("In if test; i = " + i.toString() + "; newfilename = [" + newfilename + "]; oldPart = [" + oldPart + "]; newPart = [" + newPart + "]" );
//endDebug
}
}
app.log("After looping; newfilename.trm() = [" + newfilename.trim() + "]");
return newfilename.trim();

-------------------------------------------------------------------------
That's it. Looking forward to figuring this one out! :)

Best,
DF


16/04-24 01:23
#9 : 16/04-24 12:37
Andrey
Andrey
Posts: 5
Reply to #8:

Thank you for your patience and help. Here is what is displayed

https://disk.yandex.ru/i/4-F4XRWxiEhUPg

In the screenshot, I smeared the personal data of the file, there are only numbers. There are no letters or symbols


16/04-24 12:37
#10 : 17/04-24 06:41
Delta Foxtrot
Delta Foxtrot
Posts: 106
Reply to #9:

Andrey,

I'm afraid I'm going to have to give up on this one; it's beyond my meager javascript abilities. The prebatch works perfectly, but when the script hits the "if" test to see if the filename includes "oldPart[ X ]" (the part we want to replace) it is returning FALSE every time (even though we know it should return TRUE once in the "for" loop, since the oldPart is definitely in the filename, once for each file. This leads me to conclude–whether rightly or wrongly–that the "includes()" method, our test, is in some way not handling your filenames correctly. Maybe it's the Cyrillic characters (though I doubt it, since they should be handled by your operating system), maybe something else, I just don't know.

I did try making really long filenames and it worked fine, although it would only change the first occurrence of the target phrase, but that's not the problem of course.

Hopefully someone with better scripting abilities will come along and solve this. I know I won't be able to stop thinking about it, so if something strikes me I'll be back.

If you want to try this, it's the same routine but with a DO...WHILE loop instead of a FOR loop. It also works for me, but obviously that means nothing. Leave the prebatch the same.

SCRIPT:

newfilename = item.newBasename ;
app.log("after line 1 of script; newfilename = [" + newfilename + "]; item.newBasename = [" + item.newBasename + "]" );
var i = 0
do {
if (newfilename.includes(oldPart)) {
newfilename = newfilename.replace( oldPart, newPart );
}
i++;
//debug
app.log("In if test; i = " + i.toString() + "; newfilename = [" + newfilename + "]; oldPart = [" + oldPart + "]; newPart = [" + newPart + "]" );
//endDebug
}
while ( i < app.itemCount ) ;
app.log("After looping; newfilename.trm() = [" + newfilename.trim() + "]");
return newfilename.trim();

---------------------------------------------------------------------------------------------------------------
Good luck, and I'm sorry my friend. It hurts me not to be able to solve your problem.

Best,
DF



17/04-24 06:41
#11 : 17/04-24 08:37
Andrey
Andrey
Posts: 5
Reply to #10:

https://disk.yandex.ru/i/jOVM5saHBar04w

Thanks for the effort. It's a pity it didn't work out.
The application has an "Import from CSV" function, it imports well. Is it possible to make it import directly into the "List of replacements"?


17/04-24 08:37