Swap Multiple Hyphens for a Single Hyphen

Javascript renaming examples. Javascript renaming is supported in version 3 or newer.

Swap Multiple Hyphens for a Single Hyphen

Postby PAGE » Wed Jun 15, 2016 8:52 am

I feel this should be so simple but I am failing to make any progress

So time to ask the experts

So what I am looking for it to leave ABC-XYZ as is

But replace ABC--XYZ or ABC----XYZ etc to ABC-XYZ

What I need to do is replace spaces with hyphens and then trim the hyphens

So sometimes at the moment I have ABC - DEF and ABC DEF
I want both those to become ABC-DEF

What I was going to do was change the spaces to hyphens and then trim the hyphens in a multi stage process but I have a lot of picture files so the less passes it needs the better
PAGE
 
Posts: 4
Joined: Wed Jun 15, 2016 8:46 am

Re: Swap Multiple Hyphens for a Single Hyphen

Postby Admin » Thu Jun 16, 2016 12:18 am

Use this Javascript function to do the multiple replacements all in one go (Special 14->Javascript Renaming) :

Code: Select all
String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.split(search).join(replacement);
};

var str = name;
str = str.replaceAll(" ", "-");
str = str.replaceAll("-------", "-");
str = str.replaceAll("------", "-");
str = str.replaceAll("-----", "-")
str = str.replaceAll("----", "-");
str = str.replaceAll("---", "-");
str = str.replaceAll("--", "-");
newName = str;
Admin
Site Admin
 
Posts: 1502
Joined: Tue Mar 08, 2005 8:39 pm

Re: Swap Multiple Hyphens for a Single Hyphen

Postby PAGE » Sun Jun 19, 2016 12:37 pm

Sweet thanks
PAGE
 
Posts: 4
Joined: Wed Jun 15, 2016 8:46 am

Re: Swap Multiple Hyphens for a Single Hyphen

Postby PAGE » Sun Jun 19, 2016 2:21 pm

Does this work just on filenames or folders also

I am looking to tidy up both
PAGE
 
Posts: 4
Joined: Wed Jun 15, 2016 8:46 am

Re: Swap Multiple Hyphens for a Single Hyphen

Postby PAGE » Sun Jun 19, 2016 2:23 pm

And maybe with slightly different rules

So run through the folders first and then the files - but ideally in just one pass
PAGE
 
Posts: 4
Joined: Wed Jun 15, 2016 8:46 am

Re: Swap Multiple Hyphens for a Single Hyphen

Postby Admin » Mon Jun 20, 2016 1:20 am

Yes, it can process both files and folders if you activate the option Subfolders in Filters (12)
Admin
Site Admin
 
Posts: 1502
Joined: Tue Mar 08, 2005 8:39 pm

Re: Swap Multiple Hyphens for a Single Hyphen

Postby therube » Mon Jun 20, 2016 12:49 pm

1:RegEx

Code: Select all
Match:  (.*?) *-+ *(.*)
Replace:  \1-\2
therube
 
Posts: 390
Joined: Mon Jan 18, 2016 6:23 pm


Return to Javascript Renaming