Script for renaming multiple files

Advanced Renamer forum
#1 : 07/12-20 15:05
AB
AB
Posts: 2
Hi,
I have tried renaming a bunch of files in a folder. My files follow this pattern:

fileOne_1.jpg
fileOne_2.jpg
fileOne_3.jpg
fileTwo_1.jpg
fileTwo_2.jpg
fileTwo_3.jpg
etc.

I want to rename my files like this:

fileOne_1.jpg -> newNameOne_1.jpg
fileOne_2.jpg -> newNameOne_2.jpg
fileOne_3.jpg -> newNameOne_3.jpg
fileTwo_1.jpg -> newNameTwo_1.jpg
fileTwo_2.jpg -> newNameTwo_2.jpg
fileTwo_3.jpg -> newNameTwo_3.jpg

I have a list of the names, but replacing the values with CSV import didn't seem to work, so I read on the forum that I should use the List Replace function with a script (I have over 2000 files that needs replacing, so it would be too tedious to enter them one by one).

I'm not good with scripting, so I have not been able to figure it out to make a script that searches for value X in the old name and replaces it would value Y in the new name.

Can anyone help? :)


07/12-20 15:05
#2 : 08/12-20 08:05
AB
AB
Posts: 2
Reply to #1:

I figured it out myself. Here is the script, for anyone who is having the same problem:

var str = item.name;
str = str.replace("0000083223","147738");
str = str.replace("0000087551","147739");
return str;


The syntax is:
str = str.replace("WHAT YOU WANT REPLACED","WHAT YOU WANT TO REPLACE IT WITH");

Formatting in this forum is lost, but remember to indent all lines between the first and last line of the script.


08/12-20 08:05 - edited 08/12-20 08:06
#3 : 08/12-20 13:31
David Lee
David Lee
Posts: 600
Reply to #2:
A neater method would be to put your original and replacement strings in two columns of a csv file. You can then use a simple script to simulate the List replace method but replacing only partial filenames.

The csv file goes into the List with the other filenames but you will need to ensure that it is the first in the list so that the script will read the contents before trying to make any replacements in the other filenames. The best way of ensuring this is to prefix the filename with "!", since this character precedes all other legal filename characters (apart from space) in the ASCII table - eg "!list.csv".

First you must define array variables for the match and replacement strings and a variable to keep the number of entries in the csv file. These definitions need to go in the Pre-batch script to ensure that the variables are available for each round of the main script.

For the first file in the list (ie index == 0) the main script uses the <File Line> tag to read lines from the csv file in a loop, extracting each line into two variables (using the match method with a regular expression separating on the comma) and breaking out when it reaches the end of the file.

For all other files the script extracts the two parts of each filename, again using the match method with a regular expression: "match = item.name.match(/(.*)(_.*)/);". If successful, match[1] will contain the first part of the filename and match[2] the second part - starting from "_". If the match fails (ie you have other files in the list) then match will return "null".

We then use the indexOf method to seach for the string to be replaced in the array of strings to be replaced. This will return the array element containing the string or -1 if it isn't found. If successful the script will return the corresponding new string together with the remaining part of the original filename.

BTW - you don't need to worry about indenting. That is purely for ease of reading the script - JavaScript ignores additional white space.

Original filenames:

fileOne_1.jpg
fileOne_2.jpg
fileOne_3.jpg
fileTwo_1.jpg
fileTwo_2.jpg
fileTwo_3.jpg
etc.

CSV file (eg !list.csv):

fileOne, newNameOne
fileTwo, newNameTwo
etc.

Pre batch script:

var oldStr = [];
var newStr = [];
var count = 0;

Main script:

if (index == 0) {
for (count = 0; ; count++) {
line = app.parseTags("<File Line:" + (count + 1) + ">");
if (line == "") break;
match = line.match(/(.*),(.*)/);
oldStr[count] = match[1];
newStr[count] = match[2];
}
} else {
match = item.name.match(/(.*)(_.*)/);
log(match);
if (match != null) {
n = oldStr.indexOf(match[1]);
if (n >-1) return newStr[n] + match[2];
}
}


08/12-20 13:31 - edited 08/12-20 13:58