Page 1 of 1

Changing file name w/ date,want different date format

PostPosted: Sun Mar 15, 2020 11:14 pm
by davidmeiland
Hello, I have folders of photos that were named as follows:

03_15_20 001.jpg where 3 is month, 15 is day, 20 is year 2020, and 001 is the sequence number for photos taken on that date.

I would like to change that to 2020_03_15 001.jpg.

Can anyone tell me the way to do this? I've looked at other threads on similar questions but have not been able to figure out the answer.

Thank you.

Re: Changing file name w/ date,want different date format

PostPosted: Mon Mar 16, 2020 5:47 am
by RegexNinja
Hi,
You might wanna put *.jpg into #12Filters with RegEx=Checked, then F5/Refresh to only see jpegs.
The below regex is a good example of where Negative LookArounds come in handy:

#1Regex Match/Replace
^(?!00)([01]\d)((?<!1[3-9])_(?!00)[0-3]\d(?<!3[2-9]))_((?!2[1-9])[0-2]\d)( \d+)$
20\3_\1\2\4

Month must be 2Digits, .ranging from 01-12 (not 00 or 13-upwards)
Day must be 2Digits, ...ranging from 01-31 (not 00 or 32-upwards)
Year must be 2Digits, ..ranging from 00-20 (not 21-upwards)

Results:
01_01_12 ### ---> 2012_01_01 ###
01_01_20 ### ---> 2020_01_01 ###
12_01_12 ### ---> 2012_12_01 ###
00_01_12 ### ---> No effect (bad month)
13_01_12 ### ---> No effect (bad month)
01_32_12 ### ---> No effect (bad day)
01_01_21 ### ---> No effect (bad year)

I must give credit, where credit is due.. The new, upcoming Expert's Corner Manual by trm2..
Its not yet been released, but I've been privy enough too see some of the excerpts from it.. Very nice work.
Keep an eye out for it.. It should appear in the forum's How-To section, prob at the very top.
Cheers!

Re: Changing file name w/ date,want different date format

PostPosted: Tue Mar 17, 2020 12:13 am
by davidmeiland
[quote="RegexNinja"]

Thank you very much for this, it worked perfectly, exactly what I needed.

Re: Changing file name w/ date,want different date format

PostPosted: Sat Mar 21, 2020 9:40 am
by StraightShooter
This does not work all the time because in case you dont know there is no such dates like Febuary 30 or 31.

Changing legitimate dates only

PostPosted: Thu Mar 26, 2020 9:10 pm
by RegexNinja
You're right.. I didnt even consider bad dates like Feb 3x, or April/June/Sept/Nov 31st or Feb 29 during non-LeapYears.
This matches only legitimate dates from Jan 1st 00 -> Dec 31st 20

^(?!00)([01]\d)(?<!1[3-9])_(?!00)([0-3]\d)(?<!3[2-9])(?<!4_3.)(?<!(?:0[469]|11)_31)_(?!2[1-9])([0-2]\d)(?<!4_29_(?:0[1-35-79]|1[013-57-9]))( \d+)$
20\3_\1\2\4

Re: Changing file name w/ date,want different date format

PostPosted: Thu Mar 26, 2020 10:05 pm
by RegexNinja
Sorry, I typo'd last post.. This filters all bad dates like Feb 3x, or April/June/Sept/Nov 31st or Feb 29 during non-LeapYears.
This matches only legitimate dates from Jan1 00 -> Dec31 20

^(?!00)([01]\d)(?<!1[3-9])_(?!00)([0-3]\d)(?<!3[2-9])(?<!02_3.)(?<!(?:0[469]|11)_31)_(?!2[1-9])([0-2]\d)(?<!2_29_(?:0[1-35-79]|1[013-57-9]))( \d+)$
20\3_\1\2\4

Re: Changing file name w/ date,want different date format

PostPosted: Fri Mar 27, 2020 1:02 am
by Admin
Can it be done with Javascript with an easier function? :)

Re: Changing file name w/ date,want different date format

PostPosted: Fri Mar 27, 2020 7:13 pm
by RegexNinja
I dont know about easier.. I'm still getting used to javascript, lol.. Here's what I first tried:
xxx = name.substr(0,8).split('_',3)
newName = new Date(xxx).format('{year}_{MM}_{dd}') + name.substr(8)


It did verify legitimate dates, but could've potentially inserted 'Invalid Date' into some filenames.
So then I tried creating an 'if not' for newName like this:
xxx=name.substr(0,8).split('_',3)
yyy = new Date(xxx).format('{year}_{MM}_{dd}')
if (yyy!='Invalid Date') {newName=yyy+name.substr(8)}


Worked like a charm.. Not sure if that's the easiest way to handle it though.. Too much of noob right now.
It works just like the regex, except that is doesnt filter-out the future years beyond 2020..
To be honest, Im not sure why I did that.. Its unnecessarily complicated.. Guess I got carried away, lol.

I did mean for the regex-solution to filter-out the 19xx years though, since there's no such thing as conditional replacements.
I mean no such thing as 19 OR 20 in the replacement, that's where javascript wins out on this one, handling all valid dates.

The javascript also fixes bad days: (but not months>12 or days>31)
06_31_16 -----> 2016_07_01 (Jun31->Jul01)
11_31_16 -----> 2016_12_01 (Nov31->Dec01)
02_29_17 -----> 2017_03_01 (NonLeapYear, Feb29->Mar01)

Cheers!

Re: Changing file name w/ date,want different date format

PostPosted: Sat Mar 28, 2020 1:13 am
by Admin
Nice! 8) Alternatively one could just transform the name without even going via a Date variable, but just considering everything as is, i.e. text:

Code: Select all
part=name.substr(0,8).split('_',3);
newName = '20' + part[2] + '_' + part[0] + '_' + part[1] + name.substr(8);

Re: Changing file name w/ date,want different date format

PostPosted: Sat Mar 28, 2020 1:15 am
by Admin
With separator as variable, if needed, to customize it easily:

Code: Select all
sep = '_';
part=name.substr(0,8).split(sep,3);
newName = '20' + part[2] + sep + part[0] + sep + part[1] + name.substr(8);