Keep a section of the original filename and normalize count

Advanced Renamer forum
#1 : 07/07-16 11:59
Geiras
Geiras
Posts: 2
Hey...

I've a bunch of files (~1280) that I need to rename to follow some guidelines...
The mentioned files have filenames like:

9-46-Q22-Europass-CV-ESP-20150723-John

and I need to rename it to something like this:

000046-CV_Apprenticeship_Programme


The only thing that I should keep from the original file is the first set of numbers after the first minus sign...
Currently I'm doing it by applying 4 or 5 rules but I'm sure there's an easier way... The problem with my rules are that I need to do the rename in a regular basis and there's a lot of variation in the original filename so I'm afraid that I apply the wrong rules and the files get wrongly renamed.


So, here's my rename process using theses two examples:

9-46-Q22-Europass-CV-ESP-20150723-John
25-633-Q22-CV-Europass+WJSA


1. Remove the characters at the beginning, before the first minus sign. The number of characters at the start of the filename vary.

9-46-Q22-Europass-CV-ESP-20150723-John > 46-Q22-Europass-CV-ESP-20150723-John
25-633-Q22-CV-Europass+WJSA > 633-Q22-CV-Europass+WJSA


2. Remove -Q22 which is present in all filenames
46-Q22-Europass-CV-ESP-20150723-John > 46-Europass-CV-ESP-20150723-John
633-Q22-CV-Europass+WJSA > 633-CV-Europass+WJSA

3. Remove everything after the first minus sign
46-Europass-CV-ESP-20150723-John > 46-
633-CV-Europass+WJSA > 633-

4. Add new filename
46- > 46-CV_Apprenticeship_Programme
633-> 633-CV_Apprenticeship_Programme

5.In order to make the files order correctly by filename I must guarantee that they all have the same amount of digits before the first minus sign. I add some zeros but the amount depends on how many digits already are in there. The goal is to have 6 digits before the minus sign. In the first example I added 4 zeros and in the second just 3.

46-CV_Apprenticeship_Programme > 000046-CV_Apprenticeship_Programme
633-CV_Apprenticeship_Programme > 000633-CV_Apprenticeship_Programme

---

I know I can to things this way but I was wondering if it can be done more easily and fail-proof. I tried with regular expression but I can't make it work... If anybody knows an easier one rule that renames the files it would be much appreciated...


Thanks in advance,
Geiras



07/07-16 11:59 - edited 07/07-16 12:00
#2 : 07/07-16 16:31
Tester123
Tester123
Posts: 89
I can do it using two rules:

Rule 1: (To extract the number)
Method: Replace
Text to be replaced: ^[^-]+-(\d+)-.+$
Replace with: $1-CV_Apprenticeship_Programme
Occurrence: All
Case Sensitive: No
Use regular expression: Ticked
Apply to: Name

Rule 2: (To pad the first number)
Method: Renumber
Number position: 1
Change to: Relative to existing number
Number difference: 0
Zero padding: Manual
Number length: 6
Apply to: Name

It's possible to do it with one Script method, but the rules above are built-in, so why reinvent the wheel!


07/07-16 16:31 - edited 07/07-16 16:36
#4 : 15/07-16 11:34
Geiras
Geiras
Posts: 2
Reply to #2:

Hey... It worked perfectly...
I knew there was a way to do it in much less steps than the previous method I was using...

Thank you for showing the way how it's done... Also, it helped to reduce the possibility of errors while renaming...

Thanks, thanks, thanks, thanks!! You rock!
I'll try to understand how the regular expression works in this particular case. I tried but I was missing some characters and the renaming was not working...

Once again, thank you! :)


15/07-16 11:34
#5 : 16/07-16 19:44
Tester123
Tester123
Posts: 89
Reply to #4:
If you want to understand how this regular expression works, let me help explain...

Text to be replaced: ^[^-]+-(\d+)-.+$
Replace with: $1-CV_Apprenticeship_Programme
Example filename: 9-46-Q22-Europass-CV-ESP-20150723-John

Step 1: Starting with the first and last characters ^ and $. They are anchors, where ^ means the start of the filename, and $ means the end.

Step 2: Moving on to [^-]+
The [] is a character set, and means match any one of the characters inside the set. The ^ inside a character set has a different meaning to the ^ outside a set. As we've seen, when outside it means the start of. When inside it means negation, e.g. not any of the characters. So [^-] means any character that is not a dash. The + following the set is a quantifier, and means one or more. There is another symbol * which means zero or more.

This matches the '9' from the filename an stops as the following character is a dash.

Step 3: Next is the - symbol
This is a literal character, and matches the - following the 9. This gives us '9-' so far.

Step 4: Following that is (\d+)
The parentheses group what is inside it. This is done so we can refer to it later on. The \d means any digit, and like above, the + is a one-or-more quantifier. This matches the '46' (and is known as group 1, as that is the first group defined) and stops there as the next character is a dash. So far, we have '9-46'.

Step 5: Next is the - symbol.
This is a literal character, and matches the - following the 46. This gives us '9-46-' so far.

Step 6: Finally, the .+
The dot means any character, and + is one or more. This matches the remainder of the filename, e.g. 'Europass-CV-ESP-20150723-John', and combined with the bits above, the entire filename is matched.

Now for the replacement:
$1-CV_Apprenticeship_Programme
$1 is how we refer to group 1 which was defined above (i.e. the set of digits we want to keep).
In this case $1 is replaced with '46', and the rest as just literal characters, as per your request.

Hope that helps.


16/07-16 19:44
#6 : 09/03-17 05:52
ledonegm
ledonegm
Posts: 1
It's a great topic thanks a lot


09/03-17 05:52 - edited 09/03-17 05:53