Replace sepcific characters after a specific position

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

Replace sepcific characters after a specific position

Postby RobsterUK » Wed Jan 20, 2016 12:10 pm

Hi folks,

I'm new to using Reg Ex and I think I can manage to use it for more simple tasks. But i just cannot figure out how to replace specific characters within a file name.
So I figured I'd ask advice of those more knowledgeable than myself (which is you guys :D!)

I'm trying to rename some jpg's from this format 2012-01-22 14-46-26.JPG
to this format 2012-01-22 14.46.26.JPG

Therefore replacing the dashes "-" in the second part of the file.
I have figured out how to replace all of them, or remove them, but not what I'm after. I'm stuck :(

Any advice would be greatly received.
Many Thanks in advance
RobsterUK
 
Posts: 8
Joined: Wed Jan 20, 2016 12:00 pm

Re: Replace sepcific characters

Postby Admin » Thu Jan 21, 2016 6:16 am

It can be done easily with BRU Javascript renaming:

Code: Select all
var s = name;
var index = 10;
var r = s.substr(index);
r = r.replace(/-/g, '.');
newName = s.substr(0, index) + r;


Enter this script in the Javascript Renaming dialog of BRU 3.0 then select files you want to rename.
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Re: Replace sepcific characters after a specific position

Postby RobsterUK » Thu Jan 21, 2016 2:26 pm

Thank you very much, I appreciate it!!

That worked a treat.
I suppose everything is easy once you know how to do it :wink:
RobsterUK
 
Posts: 8
Joined: Wed Jan 20, 2016 12:00 pm

Re: Replace sepcific characters after a specific position

Postby therube » Sat Jan 23, 2016 9:14 am

For a free user, does this do it?

Regex

Match:
(.+ )(\d\d)(-)(\d\d)(-)(\d\d)

Replace:
\1\2.\4.\6
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Replace sepcific characters after a specific position

Postby Admin » Sun Jan 24, 2016 11:21 am

Yes, that's another way it can be done using with regular expression!
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Re: Replace sepcific characters after a specific position

Postby RobsterUK » Mon Jan 25, 2016 10:42 am

therube wrote:For a free user, does this do it?

Regex

Match:
(.+ )(\d\d)(-)(\d\d)(-)(\d\d)

Replace:
\1\2.\4.\6


This also works
Match:
(.+)(\d\d)-(\d\d)-(\d\d)

Replace:
\1\2.\3.\4

Which is what I had to use.
When Admin posted the Java script I tested it in preview mode without actually renaming anything.
Once I came to find the time to run the code on my live files, i realised the Java rename required a licence.

So Thanks to therube for the RegEx alternative.
RobsterUK
 
Posts: 8
Joined: Wed Jan 20, 2016 12:00 pm

Re: Replace sepcific characters after a specific position

Postby Admin » Mon Jan 25, 2016 12:12 pm

Hi, yes, in general Javascript is more powerful than Regular Expressions, so I usually start thinking in Javascript terms, but sometimes Regular expression can accomplish the same, like in this case. Javascript does require a commercial license though.
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm


Return to Javascript Renaming