Remove Duplicate Word from Names

Bulk Rename Utility How-To's

Remove Duplicate Word from Names

Postby brunocalado » Wed Apr 14, 2021 8:38 pm

Hi.

I search lots of posts and none of them help me.

What I want:
Code: Select all
Changeling Changeling Exile.png -> Changeling Exile.png
Clockwork Clockwork Dragon.png -> Clockwork Dragon.png
Crab Giant Hermit Crab.png -> Crab Giant Hermit Crab.png
Deep Gnome Deep Gnome Scout.png -> Deep Gnome Scout.png
Deghuun.png -> Deghuun.png


Trying the other posts I got problems:

Problems
Code: Select all
Crab Giant Hermit Crab.png -> Crab Giant Hermit.png
Deghuun.png -> Deghun.png


Can you help me?

Thank you.
brunocalado
 
Posts: 3
Joined: Wed Apr 14, 2021 8:32 pm

Re: Remove Duplicate Word from Names

Postby therube » Wed Apr 14, 2021 9:08 pm

therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Remove Duplicate Word from Names

Postby Luuk » Wed Apr 14, 2021 10:37 pm

This very confusing, what is the rule for ...
Crab Giant Hermit Crab.png -> Crab Giant Hermit Crab.png

If the first and last words being equal, then duplicates should not be removed?
Luuk
 
Posts: 690
Joined: Fri Feb 21, 2020 10:58 pm

Re: Remove Duplicate Word from Names

Postby brunocalado » Wed Apr 14, 2021 10:51 pm



Yes. I tried these.

I replaced the _ by SPACE and this one removes the last word.
newName=name.split('_').filter(function(item,i,allItems){
return i==allItems.indexOf(item);
}).join('_');


This one with SPACE removes the wrong word.
Code: Select all
// remove multiple, leading or trailing spaces
function trim(s) {
    s = s.replace(/(^\s*)|(\s*$)/gi,"");
    s = s.replace(/[ ]{2,}/gi," ");
    s = s.replace(/\n /,"\n");
    return s;
}


var sep = ".";
str = name.split(".");
var result = [];
for(var i =0; i < str.length ; i++){
   var ent = trim(str[i]);
   if(result.indexOf(ent) == -1) {
      result.push(ent);
   }
}

newName = result.join(sep + " ");
newName += sep;
brunocalado
 
Posts: 3
Joined: Wed Apr 14, 2021 8:32 pm

Re: Remove Duplicate Word from Names

Postby brunocalado » Wed Apr 14, 2021 10:52 pm

Luuk wrote:This very confusing, what is the rule for ...
Crab Giant Hermit Crab.png -> Crab Giant Hermit Crab.png

If the first and last words being equal, then duplicates should not be removed?


Yes, sorry.
Crab Giant Hermit Crab.png -> Crab Giant Hermit Crab.png

It can't remove the Crab from the end. Only when it's duplicate like this: Crab Crab
brunocalado
 
Posts: 3
Joined: Wed Apr 14, 2021 8:32 pm

Re: Remove Duplicate Word from Names

Postby Luuk » Wed Apr 14, 2021 11:11 pm

Inside the RegEx(1) first to put a checkmark in the "v2", then to use a match and replace like...
(\b.+\b)( \1\b)
$1
Luuk
 
Posts: 690
Joined: Fri Feb 21, 2020 10:58 pm

Re: Remove Duplicate Word from Names

Postby Luuk » Wed Apr 14, 2021 11:32 pm

If you can have very many repeating duplicates in a row? This another "v2" regex to conduct them all...
(\b.+?\b)( \1\b)+
$1

And And And And And And And And And And More.txt ===> And More.txt
One Long Phrase One Long Phrase One Long Phrase.txt => One Long Phrase.txt
Luuk
 
Posts: 690
Joined: Fri Feb 21, 2020 10:58 pm


Return to How-To