Reformat the date in file names

A swapping-ground for Regular Expression syntax

Re: Reformat the date in file names

Postby sandman85 » Mon Nov 04, 2019 6:40 pm

hi all, i have a similar issue but my file are named as follow

Type_DD-MM-YYYY_name

and i would like to rename

Type_YYYY-MM-DD_name

thanks for your help
sandman85
 
Posts: 1
Joined: Mon Nov 04, 2019 6:36 pm

Re: Reformat the date in file names

Postby therube » Mon Nov 04, 2019 7:12 pm

1:RegEx
Code: Select all
Match:  (.*?)_(\d\d)-(\d\d)-(\d\d\d\d)_(.*)
Replace:  \1_\3-\2-\4_\5

Match anything up to _, non-greedy
Match 2 digits
Match 2 digits
Match 4 digits
Match everything else

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

Re: Reformat the date in file names

Postby jvanheck2 » Wed Apr 22, 2020 7:41 pm

Hi guys,

I have a similar issue although a little different. The current file name I use is:

Foto MM-DD-YYYY HH MM SS.jpg for example at this point in time in would say Foto 04-22-2020 20 39 10.jpg

I would like to rename the files to:

Foto YYYY-MM-DD HH MM SS/jpg

What would then be the formula?

Your help is very much appreciated.

BR,

Job van Heck
jvanheck2
 
Posts: 8
Joined: Wed Apr 22, 2020 7:36 pm

Re: Reformat the date in file names

Postby therube » Wed Apr 22, 2020 8:43 pm

See if this cuts it:

1:RegEx
Code: Select all
Match:  (.*)\s(\d\d-\d\d)-(\d\d\d\d)(.*)
Replace:  \1 \3-\2\4
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Reformat the date in file names

Postby jvanheck2 » Thu Apr 23, 2020 2:54 pm

Hi,

Many thanks for your swift reply. That did the trick perfectly! However, I made a mistake in my first reply.

The current filename is Foto DD-MM-YYYY HH MM SS.jpeg (instead of MM-DD).

It should still change to Foto MM-DD-YYYY HH MM SS.jpeg.

I tried to mix the numbers in the replace field somewhat but it does not seem to put the MM part before the DD part. By adding the number 2 in the replace box the tool shows both DD-MM in one go.

Any idea?

Many thanks again upfront.

BR,
Job van Heck
jvanheck2
 
Posts: 8
Joined: Wed Apr 22, 2020 7:36 pm

Re: Reformat the date in file names

Postby jvanheck2 » Thu Apr 23, 2020 3:04 pm

Euh, format should be Foto YYYY-MM-DD HH MM SS.jpeg
jvanheck2
 
Posts: 8
Joined: Wed Apr 22, 2020 7:36 pm

Re: Reformat the date in file names

Postby RegexNinja » Thu Apr 23, 2020 4:21 pm

#1Regex Match/Replace:
(.* )([0-3]\d-)([01]\d-)(19\d\d .*|20\d\d .*)
\1\3\2\4

Requires 1-space before DD and 1-space after YYYY, but does not require HH MM SS

Results:
Foto 19-05-2019 12 59 59 ---> Foto 05-19-2019 12 59 59
Foto 19-05-2019 AnyText ----> Foto 05-19-2019 AnyText
Foto 19-05-2019 --------------> no effect (no space after YYYY)
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Reformat the date in file names

Postby jvanheck2 » Thu Apr 23, 2020 5:53 pm

Fantastic, if you have no knowledge of this you are really performing magic. Thanks man!!

In my last post I have stated the filename to be Foto YYYY-MM-DD HH MM SS

What alteration in the regex should that encompass? Without knowledge it is not possible to "play" with it unfortunately. In your regex statement the year and HH MM SS are now not separated which should be the case to put YYYY upfront.

One more last thanks in advance.

BR,

Job van Heck
jvanheck2
 
Posts: 8
Joined: Wed Apr 22, 2020 7:36 pm

Re: Reformat the date in file names

Postby RegexNinja » Fri Apr 24, 2020 2:35 am

Hi.. I just play around with-it in the match, & when something works.. Heck, I just go with it, lol.
Just kidding, if you need help modding anything, just let us know..

Here's something that groups everything independently, so you can edit the replacement as needed.
Note: This regex does not mandate any spaces, or even the occurence of HH MM SS.
However, if they exist, it does match them (in case you ever need to rearrange them).

Regex Match/Replace:
^(.* )*([0-3]\d)-([01]\d-)((?:19|20)\d\d)( [12]\d [0-5]\d [0-5]\d)*(.*)$
\1\4-\3\2\5\6

Replacement Notes:
\1 = Everything before DD (if it exists, otherwise \1 in replacement has no effect)
\2 = DD (note how the - was not grouped)
\3 = MM-
\4 = YYYY
\5 = HH MM SS (if it exists, otherwise \5 has no effect)
\6 = Everything after SS (if it exist, otherwise \6 has no effect)

To mandate either Group1 or Group5, just remove its following * in the match.
If you need help strengthening or relaxing the match, just post back with details.

Results: (for \1\4-\3\2\5\6)
19-05-2019 -------------------------> 2019-05-19
Begin 19-05-2003 End -------------> Begin 2003-05-19 End
Begin 19-05-1998 12 59 59 End --> Begin 1998-05-19 12 59 59 End
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Reformat the date in file names

Postby jvanheck2 » Fri Apr 24, 2020 8:03 am

Amazing! This does it exactly as I need it to be.

Great support which is truly appreciated, and saves me like 4 hours of manual altering ;-)

Thanks a lot! Cheers!
jvanheck2
 
Posts: 8
Joined: Wed Apr 22, 2020 7:36 pm

Re: Reformat the date in file names

Postby jvanheck2 » Mon May 25, 2020 1:53 pm

Hi there again,

This post follows up on my the last post. I had my spare time ready and was trying to alter the bulk of files now. However, if I put in your expression now in Regex, the Regex box is red/orange lined which means the expression is not correct?

That is very strange asI am 100% sure that it did work last month.

The old filename is exactly Foto 01-01-2019 15 34 22.jpeg

With the expression ^(.* )*([0-3]\d)-([01]\d-)((?:19|20)\d\d)( [12]\d [0-5]\d [0-5]\d)*(.*)$ the tool is not able to perform the trick somehow.

Any idea what I am doing wrong here?

Cheers guys.

Job
jvanheck2
 
Posts: 8
Joined: Wed Apr 22, 2020 7:36 pm

Re: Reformat the date in file names

Postby RegexNinja » Mon May 25, 2020 10:31 pm

Jvanheck2
Are you putting anything in the replace-box?? It probably needs to be: \1\4-\3\2\5\6
If that doesnt work, try clicking 'reset' at the bottom-right, then put-it in there again.

If you use this expression alot, remember you can save-it as a preset by using File/SaveAs..
Just give it a decent name, then whenever you need it again, just use File/Open to load the preset.
Cheers.
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Reformat the date in file names

Postby jvanheck2 » Wed May 27, 2020 8:39 pm

Hi,

It doesnt work. I reinstalled the software but to no avail. I have several expressions brought up by boardgroup members and none of them work anymore while they did seem to work a month ago. If I copy the expression, the box is immediately redlined. Filling in the replace box or not, does not seem to matter.

Any additional thoughts an this? Unfortunately I am not able to send a screenshot. Really strange this...

BR,
Job
jvanheck2
 
Posts: 8
Joined: Wed Apr 22, 2020 7:36 pm

Re: Reformat the date in file names

Postby RegexNinja » Fri May 29, 2020 7:48 pm

All I can tell you for sure, is that your problem isnt the regex.
I just tested it out in several versions now, & results were always the same.

#1Regex Match/Replace:
^(.* )*([0-3]\d)-([01]\d-)((?:19|20)\d\d)( [12]\d [0-5]\d [0-5]\d)*(.*)$
\1\4-\3\2\5\6

Results:
Foto 01-01-2019 15 34 22.jpeg ----> Foto 2019-01-01 15 34 22.jpeg

The regex-section is supposed to 'red-line', just like any section that can effect NewNames.
Short of your names not showing up in the right-pane, I cant imagine what the problem might be.
Post back if you have more info to include.. Cheers.
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Reformat the date in file names

Postby jvanheck2 » Sat Jun 13, 2020 1:01 pm

ok thanks for your check anyway. I cant upload a picture of my situation right? Ill just keep on trying and see if i overlooked something.

BR,
Job
jvanheck2
 
Posts: 8
Joined: Wed Apr 22, 2020 7:36 pm

PreviousNext

Return to Regular Expressions