Renaming Files From a csv.
I am wanting to rename files based on information I have in a csv file. The original file names have "-3, -4, -5, etc." added on so there's not the exact same file name in that folder. Is there a way to not have to include the file names with "-3, -4, -5, etc." added on in my csv file, but still be able to read that original filename to rename as the new filename?
Example: Original filenames - "123456", "123456-3", "123456-4", "123456-5"
I want the new file names to be - "Doe_Jane-001", "Doe_Jane-002", "Doe_Jane-003", "Doe_Jane-004"
I'm trying to find a way to not have to manually add the extra file names with the "-3, -4, -5, etc." into my csv file.
A little confusing, I know. I appreciate any help.
Example: Original filenames - "123456", "123456-3", "123456-4", "123456-5"
I want the new file names to be - "Doe_Jane-001", "Doe_Jane-002", "Doe_Jane-003", "Doe_Jane-004"
I'm trying to find a way to not have to manually add the extra file names with the "-3, -4, -5, etc." into my csv file.
A little confusing, I know. I appreciate any help.
Reply to #1:
Welcome Ashley,
Do I understand your problem? I assume you have files that you want renamed like so:
123456.jpg ---> Doe_Jane-001.jpg
123456-3.jpg -> Doe_Jane-002.jpg
123456-4.jpg -> Doe_Jane-003.jpg
aabbcc.jpg ---> Deer_Jane-001.jpg
aabbcc-67.jpg -> Deer_Jane-002.jpg
aabbcc-500.jpg -> Deer_Jane-003.jpg
So you construct a CSV file like so:
123456, Doe_Jane
aabbcc, Deer_Jane
Is that correct?
If so, then I can think of no way to do this except a Script renaming method.**
(https://www.advancedrenamer.com/user_guide/v4/method_script ).
Create a new script method and paste this next code in the "Pre batch script" and apply it.
//-------------------------------------------------------
let fileData = app.readFileText ("C:\\Temp\\aren foo\\_test\\renguide.csv");
let filelines = fileData.split (/[\n\r]+/);
let renameKey = {};
filelines.forEach (zLine => {
if (zLine) {
let coldata = zLine.split (/[,;] ?/);
if (coldata[0]) {
renameKey[coldata[0]] = {"newNameRoot" : coldata[1], "rootIndex" : 1 };
}
}
} );
//-------------------------------------------------------
But change the app.readFileText line to the full path to your CSV file. Be sure to double-up ("escape") the slashes.
Then paste the following code in the "function(index, item)" block and apply it.
//----------------------------------------------------
let nameIndex = 0; // This will be reset below, if all goes well;
let fName = item.newBasename;
let fNameprefix = (fName.split (/-\d+/) )[0];
let keyData = renameKey[fNameprefix] || {"newNameRoot" : "MISSING CSV DATA!", "rootIndex" : 665 };
let newNamePrefix = keyData.newNameRoot;
//-- WARNING! The file list must be sorted by name to ensure the right file gets the right number.
if (fNameprefix === fName && keyData.newNameRoot !== "MISSING CSV DATA!") {
//-- Special case since Aren sorts names without dash *after* names with a dash.
nameIndex = 1;
}
else {
nameIndex = keyData.rootIndex + 1;
if (renameKey[fNameprefix]) {
renameKey[fNameprefix].rootIndex = nameIndex;
}
}
let suffixStr = "-" + String (nameIndex).padStart (3, '0'); // 3 total digits
return newNamePrefix + suffixStr;
//----------------------------------------------------
Then add *all* the files to Aren.
And make sure that they are sorted by Filename like so:
123456-3.jpg
123456-4.jpg
123456.jpg
aabbcc-67.jpg
aabbcc-500.jpg
aabbcc.jpg
(You can (re)sort by clicking on the "Filename" column header.)
Note that names without a dash are sorted after those with one by Aren. Don't worry, the script takes this into account.
----
You will then see that the files are renamed like as shown above.
Hope this helps,
Randy
** Not that I tried hard. JavaScript is my go-to. (^_^)
Welcome Ashley,
Do I understand your problem? I assume you have files that you want renamed like so:
123456.jpg ---> Doe_Jane-001.jpg
123456-3.jpg -> Doe_Jane-002.jpg
123456-4.jpg -> Doe_Jane-003.jpg
aabbcc.jpg ---> Deer_Jane-001.jpg
aabbcc-67.jpg -> Deer_Jane-002.jpg
aabbcc-500.jpg -> Deer_Jane-003.jpg
So you construct a CSV file like so:
123456, Doe_Jane
aabbcc, Deer_Jane
Is that correct?
If so, then I can think of no way to do this except a Script renaming method.**
(https://www.advancedrenamer.com/user_guide/v4/method_script ).
Create a new script method and paste this next code in the "Pre batch script" and apply it.
//-------------------------------------------------------
let fileData = app.readFileText ("C:\\Temp\\aren foo\\_test\\renguide.csv");
let filelines = fileData.split (/[\n\r]+/);
let renameKey = {};
filelines.forEach (zLine => {
if (zLine) {
let coldata = zLine.split (/[,;] ?/);
if (coldata[0]) {
renameKey[coldata[0]] = {"newNameRoot" : coldata[1], "rootIndex" : 1 };
}
}
} );
//-------------------------------------------------------
But change the app.readFileText line to the full path to your CSV file. Be sure to double-up ("escape") the slashes.
Then paste the following code in the "function(index, item)" block and apply it.
//----------------------------------------------------
let nameIndex = 0; // This will be reset below, if all goes well;
let fName = item.newBasename;
let fNameprefix = (fName.split (/-\d+/) )[0];
let keyData = renameKey[fNameprefix] || {"newNameRoot" : "MISSING CSV DATA!", "rootIndex" : 665 };
let newNamePrefix = keyData.newNameRoot;
//-- WARNING! The file list must be sorted by name to ensure the right file gets the right number.
if (fNameprefix === fName && keyData.newNameRoot !== "MISSING CSV DATA!") {
//-- Special case since Aren sorts names without dash *after* names with a dash.
nameIndex = 1;
}
else {
nameIndex = keyData.rootIndex + 1;
if (renameKey[fNameprefix]) {
renameKey[fNameprefix].rootIndex = nameIndex;
}
}
let suffixStr = "-" + String (nameIndex).padStart (3, '0'); // 3 total digits
return newNamePrefix + suffixStr;
//----------------------------------------------------
Then add *all* the files to Aren.
And make sure that they are sorted by Filename like so:
123456-3.jpg
123456-4.jpg
123456.jpg
aabbcc-67.jpg
aabbcc-500.jpg
aabbcc.jpg
(You can (re)sort by clicking on the "Filename" column header.)
Note that names without a dash are sorted after those with one by Aren. Don't worry, the script takes this into account.
----
You will then see that the files are renamed like as shown above.
Hope this helps,
Randy
** Not that I tried hard. JavaScript is my go-to. (^_^)