Date Transformation

Advanced Renamer forum
#1 : 02/02-20 21:02
Al Perkins
Al Perkins
Posts: 3
Hello from a newbie using this awesome program.

I am trying to rename some 500+ files to some standard format for indexing/archiving.
The filename lengths vary around 60~65 chars. The name structure/punctuation also varies according to the person naming.
Example:"Al & Suz Joint open statement as of Jan 4, 2019.pdf"
The key is to have the filename start with an ISO style YYYY-MM-DD changed from Jan 4, 2019 & without the ext cannot get past this date challenge.

Therefore from the filename example above, the final required name pattern may be "2019-01-04 JOINT.pdf"
So using the List Replace method I can transform the months "Jan" to "-01" for all months. Same for the days "4" to "-04"
So from file name "Joint -01-04 2019" I need to get to the final name above.

So given the transform so far '-01-04 2019' how do I get the year to the front AND get the fully formed pattern to the front of the filename.

If someone would be so kind to point me in the correct direction I would appreciate the help.

Oh, I forgot to mention I don't speak REGEX.

Thank You
A


02/02-20 21:02 - edited 03/02-20 19:02
#2 : 03/02-20 14:25
David Lee
David Lee
Posts: 343
The easiest way is to use a Regular Expression and you can also get away with only one List replace method...

1) Use "List replace" to convert months to 2-digit numbers
2) Use "Renumber" to add a leading zero to the day
3) Use "Replace" with a Regex to fix everything else

1) List replace... You already have this - but you don't need to add the hyphen.

2) Renumber ...
Number position: 2
Change to Relative to existing number
Zero padding: Manual
Number length: 2

3) Replace ...
Text to be replaced: .*(\d{2}) (\d{2}), (\d{4})$
Replace with: \3-\1-\2 JOINT
Use regular expressions: √

It's well worth learning to use Regex - they may seem daunting at first but you should rapidly get the hang of them. Kim's User Guide is a good place to start: www.advancedrenamer.com/user_guide/regular_expresions
and you will be able to find more information online using a web search for Regex - but note that Advanced Renamer uses the "Perl Compatible" flavour (PCRE).

To get you started - here is an explanation of the above Regex: .*(\d{2}) (\d{2}), (\d{4})$

"." matches any character and "*" means any number of repetitions (including zero).
"\d" matches a numeric digit and {x} defines the number of repetitions to match.
"$" in this context means that the entire sequence must be at the end of the filename.
The spaces and comma have their normal meaning.

Parentheses "(...)" define "sub-expressions" and the characters matched by each of these are saved into numbered variables: \1, \2, \3 .... that you can use in the "Replace with" string.

I hope this is helpful.


03/02-20 14:25 - edited 03/02-20 14:27
#3 : 03/02-20 15:03
David Lee
David Lee
Posts: 343
Reply to #2:

The above solution has a potential flaw: if any of the 3-letter month strings were to appear elsewhere in the filename (eg if you had Al & Jan rather than Al & Suz) they would also also be converted into 2-digit numbers. It looks like this is unlikely to be a problem in your case but the issue can be eliminated by putting the "Replace" method at the top of the list - with suitable modifications - which will remove any spurious "months" before the List replace method..

So:

1) Replace:. Text to be replaced: .*(\w{3}) (\d{1,2}), (\d{4})$
2) Renumber: Unchanged
3) List replace: Unchanged

In the modified Regex...
\w{3} = match 3 alpha characters
\d{1,2} = match from 1 to 2 numeric characters


03/02-20 15:03 - edited 03/02-20 15:09
#4 : 05/02-20 15:23
Al Perkins
Al Perkins
Posts: 3
Reply to #3:
Many Thanks for your detailed explanation David.
Your solution works like a charm and is far more elegant than my attempt.

I did get my attempt to work ultimately after many tries with 3 ListReplace, 2 Moves and a Trim but that revealed other naming anomalies whereby the month was spelled in long form requiring another set of listreplaces. Good training for me.

I do have Levithan/Goyvaerts cookbook but that's far from an easy read.
Many Thanks Again.
A


05/02-20 15:23
#5 : 05/02-20 17:37
David Lee
David Lee
Posts: 343
Reply to #4:
Even more elegant would be a single Script method...

In the Pre batch script:

var months = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

Main script window:

date = item.name.match(/.*(\w{3}) (\d{1,2}), (\d{4})$/);
month = ('0' + months.indexOf(date[1])).slice(-2);
day = ('0' + date[2]).slice(-2);
year = date[3];
return year + '-' + month + '-' + day + ' JOINT';


05/02-20 17:37 - edited 05/02-20 17:39
#6 : 05/02-20 23:35
Al Perkins
Al Perkins
Posts: 3
Reply to #5:
Wow, I did not expect you to invest your time further but a much better solution.
I will try this out. Thank You.

A



05/02-20 23:35 - edited 05/02-20 23:36