Move everything after the last instance of an underscore

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

Move everything after the last instance of an underscore

Postby rfexchch » Wed Feb 17, 2016 3:30 am

Hi, I'm trying to move everything that occurs after the last instance of an underscore '_' to the front of the filename and would be grateful for some assistance.
I have two versions of my filenames (unfortunately!):

From:

Version 1
Abbreviated journal name_Full article name_Author(s) Date.pdf
e.g. ABR_An empirical study of blah_Smith et al 2015.pdf

Version 2
Abbreviated journal name_Part 1 of article name_Part 2 of article name_Author(s) Date.pdf
e.g. ABR_An empirical study of blah_Evidence from the public sector_Smith et al 2015.pdf

To:
Version 1
Authors Date_Abbreviated journal name_Full article name.pdf
e.g. Smith et al 2015_ABR_An empirical study of blah.pdf

Version 2
Author(s) Date_Abbreviated journal name_Part 1 of article name_Part 2 of article name.pdf
e.g. Smith et al 2015_ABR_An empirical study of blah_Evidence from the public sector_.pdf

Thank you
rfexchch
 
Posts: 2
Joined: Wed Feb 17, 2016 3:14 am

Re: Move everything after the last instance of an underscore

Postby Admin » Wed Feb 17, 2016 7:37 am

If you have a commercial license for BRU this can be done easily using Javascript.
Admin
Site Admin
 
Posts: 1504
Joined: Tue Mar 08, 2005 8:39 pm

Re: Move everything after the last instance of an underscore

Postby rfexchch » Wed Feb 17, 2016 10:03 am

Many thanks.
rfexchch
 
Posts: 2
Joined: Wed Feb 17, 2016 3:14 am

Re: Move everything after the last instance of an underscore

Postby RobsterUK » Wed Feb 17, 2016 12:54 pm

Or, if you want to use Reg Ex, then this should work if all files are the same format as the examples you posted.

Reg Ex:
Code: Select all
Match
^(\w+.*)_(\w+) (\w+) (\w+) (\d\d\d\d+)$

Replace
\2 \3 \4 \5_\1


Try it out :wink:
RobsterUK
 
Posts: 8
Joined: Wed Jan 20, 2016 12:00 pm

Re: Move everything after the last instance of an underscore

Postby Admin » Wed Feb 17, 2016 11:37 pm

This is the Javascript function :

Code: Select all
var char2find = "_";
var index = name.lastIndexOf(char2find);
newName = name.substr(index+1) + char2find + name.substr(0,index);


Copy and paste this into Javascript Renaming, then rename files.

This Javascript function has no constraints on file name format, e.g. it switches everything around the last "_", or any other character of choice.
It works with any number of _ in the file name or any position, also works for files like :
_testfile.ext
or
test_file_.ext

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

Re: Move everything after the last instance of an underscore

Postby hns » Sat Feb 27, 2016 9:36 am

You may simply use following in RegEx option:
(.*)_([^_]*)
Replace with:
\2 \1
Second block is anything not having an underscore. It is preceded by an Underscore which is preceded by anything (including Underscore). Hence the second block is the string after the last underscore.
Hope it helps.
- HNS
hns
 
Posts: 2
Joined: Sat Feb 27, 2016 9:18 am

Re: Move everything after the last instance of an underscore

Postby spamspambaconspam » Mon Feb 29, 2016 1:51 pm

Simple?

Here is simple:

Match:
(.*)_

Replace:
\1

That's all!

____________________________________________________________________________________

How it is working:

The pattern is saying...

From the file name, I want a capture a chunk of characters
and each character in that chunk can be anything that might be in that slot (i.e.: letters, numbers, spaces, weird stuff, whatever...)
and the whole chunk is allowed to be zero or more characters in length.

pattern items:
the parenthesis (capture this)
the dot (anything)
for zero or more times (asterisk)

THEN, IT ADDS THIS CAVEAT:

So long as the "run" of characters you've captured IS followed by an underscore.

pattern item:
the underscore that is placed OUTSIDE of the capture, at the end


To play around with it, if you also put an underscore BEFORE the parenthesis, you'd only capture what's AFTER the first underscore and BEFORE the last underscore.
In your examples, that would lop off the "ABR_" piece.

I love RegEx.
I need a 12 step program! :D

Have fun!

Cheers!

~spamster
spamspambaconspam
 
Posts: 5
Joined: Mon Nov 03, 2014 12:47 am

Re: Move everything after the last instance of an underscore

Postby Admin » Tue Mar 01, 2016 3:03 am

Yes but the request was to rotate the name around _ , not to crop. So you will need to capture the part after the _ too.
Admin
Site Admin
 
Posts: 1504
Joined: Tue Mar 08, 2005 8:39 pm


Return to Javascript Renaming