Adding file numbers based on characters in filename

A swapping-ground for Regular Expression syntax

Adding file numbers based on characters in filename

Postby roybridge » Fri Aug 16, 2019 9:00 am

Hi,

I could really use some help. I have a lot of files that contain (but don't end with) a # symbol immediately followed by one or two digits (e.g. #9 or #30). What I need is to prefix the filenames with two digits which relate to the number after the #. If the number after the # is a single digit, I need my filename to start with a 0.

Kind regards
Chris
roybridge
 
Posts: 3
Joined: Fri Aug 16, 2019 8:54 am

Re: Adding file numbers based on characters in filename

Postby therube » Fri Aug 16, 2019 11:40 am

There's probably a way to handle it in one "go", but it's not hitting me, so we'll do it twice.


#-single-digit
1:RegEx
Code: Select all
Match:  (.*)(#)(\d)$
Replace:  \20\3---\1


#-double-digit
1:RegEx
Code: Select all
Match:  (.*)(#)(\d\d)$
Replace:  \2\3---\1


Match everything up to the last #, followed by 1 or 2 digits
# symbol is matched separately
As are the final digit(s)

Then just rearrange things.
As it is, not knowing what you've wanted, I added dashes to highlight the change at the beginning of the name.
You may want a single space or whatever.
Likewise, I've removed the #\d+ from then end of the name, not sure if you wanted to retain them?
If you do then just change the Replace: to include them back in, \2\3---\1\2\3.
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Adding file numbers based on characters in filename

Postby roybridge » Fri Aug 16, 2019 11:42 am

Perfect - thank you :D
roybridge
 
Posts: 3
Joined: Fri Aug 16, 2019 8:54 am

Re: Adding file numbers based on characters in filename

Postby roybridge » Fri Aug 16, 2019 11:50 am

ALMOST perfect. It doesn't seem to want to work if there are characters following, for example:-

Test Day #13.mp4 renames ok but Test Day #13(Part 1).mp4 or Test Day #13 (REVISED).mp4 don't.
roybridge
 
Posts: 3
Joined: Fri Aug 16, 2019 8:54 am

Re: Adding file numbers based on characters in filename

Postby therube » Sun Aug 18, 2019 5:57 pm

Depending on what you have... any number of ways to go about it... might require some "clean up"...
Don't know that there's going to be one way... Going to be consequences regardless of what you do...
I would take those that did not get renamed, i.e. those that have trailing...

For (only) those that didn't work above, you could use something like:

Match: (.*)(#)(\d+)(.*)
Replace: \20\3---\1

That would truncate everything after the final set of #-digit(s) combination.
So #1, #12, #123, #123456789 ...
As you can see, that does not check for specifically 1 or 2 digits, which you could do.
(But that brings it's own problems.)
You might want to retain that trailing part, otherwise you may run into duplicate situations, or you may just want it...
In which case you could change the Replace to: \20\3---\1\4

And, as written, that would also cause an extra 0 to be in the name:
Jessica Jones V2016 #2 (2016) - Copy#30.cbz
->
#030---Jessica Jones V2016 #2 (2016) - Copy.cbz

Which you could then use a RegEx to remove that extra 0.
Code: Select all
Match:  ^(#0)(\d\d.*)
Replace:  #\2

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


Return to Regular Expressions