Swap words in jumbled filenames

Advanced Renamer forum
#1 : 20/06-22 12:59
Ishan Gupta
Posts: 4
I have a folder that has following type of filenames

Waifu-Character_Megumin-Copyrights_Konosuba-Author_Mossi
Waifu-Author_Sciamano240-Character_TifaLockhart-Copyrights_FinalFantasy

I want all my filenames to be like this.

Waifu-Konosuba-Megumin-Mossi
Waifu-FinalFantasy-TifaLockhart-Sciamano

Ive been trying to use some sort of regex for a long time but couldnt figure out what to do. Any help will be very appreciated.
#2 : 20/06-22 19:54
David Lee
Posts: 1125
It will be easiest to use a script method:

name=item.name;
genre = name.match(/^[^-]*/);
copyrights = name.match(/Copyrights_([^-]*)/)[1];
character = name.match(/Character_([^-]*)/)[1];
author = name.match(/Author_([^-\d]*)/)[1];
return genre + "-" + copyrights + "-" +character + "-" + author;

edited: 20/06-22 19:57
#3 : 20/06-22 20:43
Ishan Gupta
Posts: 4
Reply to #2:
Thanks for the reply. I was able to write my own script too if someone else is also looking for something similar (Hard coded to my case tho)

name = item.newBasename;
result = name.split("-");
fin='Waifu';

for (index = 0; index < result.length; ++index) {
value = result[index];
if (value.substring(0, 3) === "Man") {
fin = fin + "-" + value;
break;
}
}

for (index = 0; index < result.length; ++index) {
value = result[index];
if (value.substring(0, 3) === "Cop") {
value = value.substring(11);
fin = fin + "-" + value;
break;
}
}
for (index = 0; index < result.length; ++index) {
value = result[index];
if (value.substring(0, 3) === "Cha") {
value = value.substring(10);
fin = fin + "-" + value;
break;
}
}
for (index = 0; index < result.length; ++index) {
value = result[index];
if (value.substring(0, 3) === "Aut") {
value = value.substring(7);
fin = fin + "-" + value;
break;
}
}
return fin
#4 : 21/06-22 00:33
David Lee
Posts: 1125
Reply to #3:

This is unnecessarily complicated!

Your script suggests that all the tags are optional and there is a fourth possibility with tag name starting with "Man" (I'm calling it "ManXXX" in the following).

The following script will achieve the same as yours, based on the match method using regular expressions:

name=item.newBasename;
newName = item.name.match(/^[^-]*/);
if (match=name.match(/ManXXX_([^-]*)/)) newName += ('-' + match[1]);
if (match=name.match(/Copyrights_([^-]*)/)) newName += ('-' + match[1]);
if (match=name.match(/Character_([^-]*)/)) newName += ('-' + match[1]);
if (match=name.match(/Author_([^-]*)/)) newName += ('-' + match[1]);
return newName;