File rename hierarchical with RegEx help

Post Reply
Arthur
Member
Posts: 114
Joined: Sat Sep 09, 2017 11:58 pm
Location: Yateley, UK

File rename hierarchical with RegEx help

Post by Arthur »

Dear clever heads;
As this is my first post, I would like to say hello in the first place.
I am trying to sort out file renaming with RegEx, but as I am a novice on this, it is not so straightforward as one would have thought, therefore I dare to ask for help in here.

Files need to be renamed with a hierarchical format

01.01_Filename.ex
02.01_Filename.ext (...)
10.01_Filename.ext
10.10_Filename.ext

Initial filenames are the whole variety of options, starting from:

01. filename.ext
1 file name.ext
1.1. filename.ext
5.13 filename.ext
etc.

to some more sophisticated ie:
Section 1 filename.ext

Is it possible for a Regular Expression to search the first say 5 characters in a file name (only digits, excluding letters) for single digits and if they are single, add 0 before, but if they are double digits skip them, while if there is no digit after the dot (ie. 01 filename.ext) - add 00 or whatever is required to match the input mask??
The 'filename' part is to be intact, so in essence it is only about the hierarchy numbering at the beginning of the file name.

If anything is not clear enough in this brief explanation, by all means please ask.
Thank you in advance.
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: File rename hierarchical with RegEx help

Post by Padawan »

I have some experience with regular expressions, although I'm certainly not the biggest expert. Personally, I would not try to do it in a single regular expression.

You can use up to 5 search and replace regular expressions in a row in one Rename Job element, and nothing is stopping you from using multiple Rename jobs elements in a row.

I would do something like this:

Step 1:
A search and replace regular expression to add a dot if necessary, to fix these cases:
1 file name.ext
Search regex: ^(\d\d?) (.*)
Replace regex: \1. \2

Step 2:
A search and replace regular expression to add a second digit after the dot if there is none, to fix these cases:
01. filename.ext

Search regex: ^(\d\d?)\. (.*)
Replace regex: \1.0 \2

Step 3:
A search and replace regular expression to remove the extra dot, to fix these cases:
1.1. filename.ext

Search regex: ^(\d\d?)\.(\d\d?)\. (.*)
Replace regex: \1.\2 \3

Step 4:
2 search and replace regular expressions to add leading zero's. (It might be possible with only one, but I always do it with two different search and replace regular expressions. One expression to add zero's before each number, one expression to remove the obsolete ones)

Search regex 1: ^(\d\d?)\.(\d\d?) (.*)
Replace regex 1: 0\1.0\2 \3

Search regex 2: ^0?(\d\d)\.0?(\d\d) (.*)
Replace regex 2: \1.\2 \3

I like this approach because it is very readable. You might be able to do it in a single, more complex expression, but if you look at it again in a year time you will have more trouble understanding it compared to using 4 or 5 simple expressions who only do one single task.

(For testing purposes I even use different rename jobs for each search and replace. This way I can see the intermediate results. Once everything works ok I put it all in one rename job.)
Post Reply