Batch remove duplicated word in folder/filename?

A swapping-ground for Regular Expression syntax

Batch remove duplicated word in folder/filename?

Postby glennrune » Thu Dec 28, 2017 4:59 pm

Hello.

I got different folder name who is the same many times (repetition) is it a way to remove the word who are written many times, and only keep one?
Is there a way to batch remove duplicated word in folder/filename? :?:

Original filename:
2008 - Greatest Hits. Classic Remixes. Classic Remixes. Classic Remixes. Classic Remixes. Classic Remixes. Classic Remixes. Classic Remixes. Classic Remixes. Classic Remixes. Classic Remixes. Classic Remixes. Classic Remixes
1991 - Mcmxc A.D. (The Limited Edition). (The Limited Edition). (The Limited Edition). (The Limited Edition). (The Limited Edition). (The Limited Edition). (The Limited Edition). (The Limited Edition). (The Limited Edition)

New filename:
2008 - Greatest Hits. Classic Remixes.
1991 - Mcmxc A.D. (The Limited Edition)
glennrune
 
Posts: 7
Joined: Thu Dec 28, 2017 4:45 pm

Re: Batch remove duplicated word in folder/filename?

Postby therube » Fri Dec 29, 2017 4:41 pm

As a start... Find Repeated Words.


I had (have) a similar need, where I'd want to remove the duplicated -u#### string.
And I came across some code that looked like it was going to work just fine, but at the time I said to myself, "oh, I'll hold off for now", thinking that as I had the code & I could come back to it when I felt like looking into it further. So just where did I store that particular code :?: And looking again more recently, I'm not finding that same code, & what I'm coming up with currently, while working for most situations, isn't quite correct for all situations, so I'll keep trudging away.

Code: Select all
20.23-u51155941-u51155941.mp3
20.29-u182566304-u182566304.mp3
20.59-u100977928-u100977928.mp3

53.04-u61685880-u16228484-u61685880.mp3
20.05-u12253723-mp3-u73959350-mp2-testload.mp3


Something like:

1:RegEx
Code: Select all
Match:  (.*)(-u\d.*)
Replace:  \1

Would simply truncate everything after the final -u###,
but in the case of the last two examples,
if the -u numbers differ, I'd want to keep them,
& if the -u numbers were not consecutive, I'd also want to delete the dup'd -u, but keep intervening text.
therube
 
Posts: 534
Joined: Mon Jan 18, 2016 6:23 pm

Re: Batch remove duplicated word in folder/filename?

Postby Admin » Sat Dec 30, 2017 12:47 am

Try this:

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;
Admin
Site Admin
 
Posts: 1579
Joined: Tue Mar 08, 2005 8:39 pm

Re: Batch remove duplicated word in folder/filename?

Postby Admin » Sat Dec 30, 2017 12:48 am

It will remove duplicate text , where "text" is separated by a .
Admin
Site Admin
 
Posts: 1579
Joined: Tue Mar 08, 2005 8:39 pm

Re: Batch remove duplicated word in folder/filename?

Postby glennrune » Fri Jan 05, 2018 5:58 pm

Thank you for answare.

Should I enter the code in «JavaScript» section?
And do I have to purchase a commercial license to use this function?
glennrune
 
Posts: 7
Joined: Thu Dec 28, 2017 4:45 pm

Re: Batch remove duplicated word in folder/filename?

Postby Admin » Sat Jan 06, 2018 1:06 pm

Should I enter the code in «JavaScript» section?


yes Special (14)

And do I have to purchase a commercial license to use this function?


You can test it without commercial license and see the preview for it, but to actually do the renaming with Javascript a commercial license is needed.

thanks
Admin
Site Admin
 
Posts: 1579
Joined: Tue Mar 08, 2005 8:39 pm

Re: Batch remove duplicated word in folder/filename?

Postby therube » Wed Jan 31, 2018 6:28 am

My use-case examples, above, would be better served by:

Code: Select all
Match:  (.*)-u(\d+)(.*)-u\2(.*)
Replace:  \1-u\2\3\4


Though that will still fail on:
Code: Select all
-u144055676-u142099913-u144055676
therube
 
Posts: 534
Joined: Mon Jan 18, 2016 6:23 pm

Re: Batch remove duplicated word in folder/filename?

Postby bru » Fri Mar 23, 2018 10:00 pm

glennrune, try:
(.*?)( .{5,}?)\2
\1\2
Warning: It removes removes the repeats (5-or-more chars) and everything that follows, but it looks like thats what you need?
Works fine if your repeats begin with a space separator.

therube, try:
(.*?)(-u\d+)(.*)\2(.*)
\1\2\3\4
The ? keeps Group1 from grabbing so much.

Cheers!
bru
 
Posts: 16
Joined: Wed Jan 31, 2018 7:35 pm

Re: Batch remove duplicated word in folder/filename?

Postby therube » Sun May 19, 2019 2:23 pm

Some things I came up with to meet particular needs of mine:

regex: is what I used to search (using Everything) to find files I want to rename
Match: & Replace: are the codes used to effect the renames in BRU

Code: Select all
regex:(\-\d\d\d\d\d\d\d\d\d\d)(\1)
   test-0585563124-0585563124.mp3
Match:  (.*?)-(\d\d\d\d\d\d\d\d\d\d)
Replace:  \1-\2
   test-0585563124.mp3


regex:(\-u\d\d\d\d\d\d+)(\1)
   test-u112019849-u112019849.mp3
Match:  (.*?)-(u\d\d\d\d\d\d+)
Replace:  \1-\2
   test-u112019849.mp3


regex:(\-\d\d+)(\1).dk
regex:(\-\d\d\d)(\1).dk
   test-446-446.dk.mp3
Match:  (.*?)-(\d\d\d)(.*dk)
Replace:  \1-\2
   test-446.mp3


regex:\b(-u\d+).*\1\b
   test-u109375941-Td.bm-u109375941 (OL-424).mp3
Match:  (.*?)(-u\d+)(.*)(\2)(.*)
Replace:  \1\2\3\5
   test-u109375941-Td.bm (OL-424).mp3

(Heh. And I [now] see, that my 3rd example is exactly what bru came up with [basically]. Though at the time I didn't really understand it. Now I understand it - a little.)
therube
 
Posts: 534
Joined: Mon Jan 18, 2016 6:23 pm


Return to Regular Expressions