Remove keywords (from a file) for all files in list.

I have been trying to use Advanced Renamer for my simple use case but I have hit a wall. My situation and use case seems simple.

I have a text file with a list of over 200 keywords, each keyword is on it's own line. All I need is for AR to access that list of keywords and then remove any keywords in that list from any and all file names in my file list.

I came to the conclusion that this task has to be done via the scripting method with AR, there is no other way (correct me if I'm wrong). So, I used the following script and AR gave the the error "ReferenceError: ActiveXObject is not defined"

Below is the script I used. I am at a loss and would appreciate some help either modifying this script to work or use another method to (quickly) get this simple task done in AR.

// Load keywords from external text file
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.OpenTextFile("X:\\path\to\keywords.file", 1);
var keywords = [];
while (!file.AtEndOfStream) {
var line = file.ReadLine();
if (line.length > 0) {
keywords.push(line);
}
}
file.Close();

// Function called for each filename
function rename(name) {
for (var i = 0; i < keywords.length; i++) {
var kw = keywords[i];
// Remove *all* occurrences of the keyword anywhere in the filename
while (name.indexOf(kw) !== -1) {
name = name.replace(kw, "");
}
}
return name; // cleaned filename
}



Thanks.

EDIT: I also just tried the following script and get error "ReferenceError: File is not defined ", but the file is defined.


// Read the content of your keyword file
var keywordsFile = "X:\\path\to\keywords.file";
var keywordsContent = File.read(keywordsFile);
var keywords = keywordsContent.split('\n').map(function(line) {
return line.trim(); // Remove leading/trailing whitespace
}).filter(function(line) {
return line.length > 0; // Filter out empty lines
});

// Get the current filename
var newFileName = item.newName;

// Iterate through keywords and remove them
for (var i = 0; i < keywords.length; i++) {
var keyword = keywords[i];
// Use a regular expression to replace all occurrences of the keyword
// The 'g' flag ensures all occurrences are replaced, 'i' for case-insensitivity if desired
var regex = new RegExp(keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'); // Escape special characters for regex
newFileName = newFileName.replace(regex, "");
}

// Set the new filename
return newFileName;
Reply to #1:

Note that Aren JS is closer to pure ECMAScript. It doesn't have (all of) the extra APIs of a browser -- and certainly not Node.js.

But it *does* have a readFileLines function ( https://www.advancedrenamer.com/user_guide/v4/method_script ).

Here's how to use it for your task:

1) Paste this as a "Pre batch script":
//----------------------------------------------------
//-- You must escape slashes in the file path:
badwordsArry = app.readFileLines ("C:\\Temp\\aren foo\\_test\\badwords.txt");
badwordsArry = badwordsArry.filter (zWord => zWord != ""); // Need to strip empty lines.

badwordsRegx = badwordsArry.map (zWord => {
let purePhrase = zWord.replace (/\r|\n/g, ""); // readFileLines leaves line endings in array
return new RegExp (`\\b${purePhrase}\\b`, "ig"); // \b in the regex avoids many false positives
} );
/* For debug:
console.log (badwordsArry);
console.log (badwordsRegx[0].toString() );
//*/
//----------------------------------------------------

2) Then for the regular (per file) script, use:
//----------------------------------------------------
let fName = item.newBasename;

badwordsRegx.forEach (zRegx => fName = fName.replace(zRegx, "") );

return fName;
//----------------------------------------------------

Regards,
Randy

ETA: Also be sure that your external file's extension is allowed in:
Settings -> JS Script editor -> Allowed extensions for reading content from files
Reply to #2:

Thanks for your time.

Your script doesn't entirely work for me. I didn't mention that all the keywords in my list end in a comma and space (ie. keyword, ).

I never ran into a problem when keywords are at the beginning of a filename. The problem is with keywords in the middle or end of a file name and it seems totally random.

The fact that my keywords in my file all have a trailing comma and space obviously matters because if remove the comma and space from the keywords in my list your script runs fine (obviously the comma and space are not removed, though).

However with my keyword list left as is, here are the combinations that do and don't work.

Keywords are removed in the following situations:

- word keyword, word - word.ext

- keyword, keyword, .ext (both keyword instances are removed)

- keyword1, keyword2, .ext (both keywords are removed)

- keyword, word - keyword, word.ext (both keyword instances are removed)

- word keyword, word.ext

- word, keyword, - word.ext



Keywords are not removed in these situations:

- word, keyword, - keyword, word.ext (only second keyword instance is removed)

- word, keyword1, - keyword2, word.ext (neither keyword is removed)

- word - keyword, - word.ext

- word, keyword, - word.ext


I see no logic as to why it works and why it doesn't.


Log shows...

"keyword1, \r",
"keyword2, \r",
"keyword3, \r",
"keyword4, \r",
"keyword5, \r",
"keyword6, \r",
]
/\bkeyword1, \b/gi
Reply to #3:

Okay, if I read you correctly, you *do* want the comma and space removed from the filename, right?

Anywho, change the return line to:
return new RegExp (`${purePhrase}`, "ig");

The trailing \b in the regex was literally hitting a (word) edge case. Pun intended ;)

But, since you can use regex yourself in the badwords file, you can specify \b, etc. there -- if it is ever needed for your particulars.

Speaking of which, if your badwords file really contains: "keyword1, ", "keyword2, ", "keyword3, ", etc.
You could replace all that with a single line of "keyword\d, " . -- Using the JS regex token