#1 : 14/04-24 03:00 RJ Barber
Posts: 2
|
I have the following script that for each file in the list is supposed to loop through a list of strings and replace them with nothing. The goal is to remove about 30+ strings from 1000s of file names, but in the order they appear in the substrings variable, which is why I'm using this script to execute remove methods in the order of the substrings.
When I run the script, the status indicates OK for all files, but all the file names are the same\in the New Filename column. Auto Test is turned on. This is my first foray into scripting for Advanced Renamer, but I write powershell and some javascript so I understand enough to be dangerous but apparently not enough. Looking for help as to why this script is not producing file name changes. Script: // Define the list of substrings to remove var substrings = [ ".photographycommercial", ".photographypeople", ".photographylandscape", ".photographyportrait", ".photographyarchitecture", ".photographyblackandwhite", ".photographyadvertising", ".photographyabstract", ".photographydocumentary", ".photographymountainscape", ".photographyexperimental", ".photographynature", ".photographyengagement", ".photographystreet", ".photographyfineart", ".photographyjournalistic", ".photographyevent", ".photographystilllife", ".photographywildlife", ".photographyfashion", ".photographycouples", ".photographysenior", ".photographyseascape", ".photographynewborn", ".photographyconceptual", ".photographygrunge", ".photographywedding", ".photographyequipment", ".photographycommunity", ".photographyretro", ]; // Function to remove substrings from file names function removeSubstrings() { // Iterate over each file in the list for (var i = 0; i < app.itemCount; i++) { // Get the file name var fileName = app.getItem(i).newName; // Iterate over each substring for (var j = 0; j < substrings.length; j++) { // Remove the substring from the file name fileName = fileName.replace(substrings[j], ""); } // Set the modified file name app.getItem(i).newName = fileName; } } // Call the function to remove substrings removeSubstrings(); |
#2 : 14/04-24 03:56 Delta Foxtrot
Posts: 364
|
Reply to #1:
Hi RJ, welcome, This should work: Pre-batch script (in the prebatch script this will only execute once, instead of each time you step through the files): var substrings = ["Whatever","you","need"]; Script: var myName = item.newName; for (var i = 0; i < substrings.length; i++){ if ( item.newName.includes(substrings)){ myName = myName.replace(substrings,"") } } return myName; ------------------------------------------- Try it; it worked for me on a mini-test with a few files and a few array elements. Let us know how you fare. EDIT: You might want to return "trim(myName)"... if any of your strings cause spaces at start or end. ENDEDIT Best, DF |
#3 : 14/04-24 05:14 Delta Foxtrot
Posts: 364
|
Reply to #2:
Oh, as to why your script didn't change anything. I'm not great at jscript either, so take everything that follows with a half-grain of salt. I have written a bit of js in the version here in ARen. I try to stay away from functions within the main function; I've never had success doing that, although I think I saw a thread at one time where Kim Jensen, the author, said you could use them. So... Anyway, and Kim can correct me if I'm wrong, I believe your for loop was faulty. ARen executes your script all the way through on a filename (item.name) before going to the next filename, so app.itemCount *I don't believe* does you any good here. I looped instead through the elements of the array for each filename, extracting any element found in the if statement. Oh, I used "item.newName" because, if you have any methods above the script, that's what ARen passes from the last previous method. It will keep the script from breaking if you later add methods before the script. Again, grain of salt... :) Best, DF |
#4 : 17/04-24 07:45 RJ Barber
Posts: 2
|
It got me closer, but weird behavior, many files ended up with double extensions. Had to make about 20 more Replace methods to remove the the extra extension, which was added to the file name portion. Tried everything I could think of to get a revision that would stop that behavior but to no avail. Tried isolating the extension and filename, then changing the filename and adding the extension back, and the same extensions that were doubled in the results continued no matter what I did with the script. My task is done but I could not get it done with just the script. Thanks for all your help!!
|