How to reformat existing date in a filename?

Bulk Rename Utility How-To's

How to reformat existing date in a filename?

Postby SteverinoLA » Tue Aug 30, 2022 11:21 pm

I have a bunch of files with dates in the file name but not in the format or location I need. I've spend a few hours w/this app but haven't figure out how to do it, probably b/c I'm a regular expression noob.

Here are some sample files:

    03.15.22 Executive Board Meeting.pdf
    General Session Agenda - 02.15.22.pdf

I want to rename all of these so that that are formatted like this:

    2022-03-15 Executive Board Meeting.pdf

Can this be done using this pretty amzing app?

Thanks,
Steve
SteverinoLA
 
Posts: 1
Joined: Tue Aug 30, 2022 11:12 pm

Reformat yy.mm.dd --> 20yy-mm-dd and move to front

Postby Luuk » Wed Aug 31, 2022 1:55 am

Greetings Steve. With the 1 example new-name, Im just assuming that 1 "space" should replace any date-separators for the new-names?
Im also assuming dates like yy-mm-dd with "yy" ranging 00-29, and the names always using either spaces or -'s as the date separators?
So first to put a checkmark inside for "v2", then the "Match" and "Replace" could be like...

(?:(?<=^)|(?<=[- ]))(\d\d)\.(\d\d)\.([012]\d)(?=[- ]|$)(?X)(.*)(?<![- ])[- ]*:(20\d\d-\d\d-\d\d)[- ]*(?X)( $| (?= ))
:20$3-$1-$2(?X)$2 $1 (?X)

03.15.22 Executive Board Meetin.txt ================> 2022-03-15 Executive Board Meetin.txt
General Session Agenda - 02.15.22.txt ===============> 2022-02-15 General Session Agenda.txt
General Session Agenda - 02.15.22 - SomeName.txt ===> 2022-02-15 General Session Agenda SomeName.txt
Luuk
 
Posts: 706
Joined: Fri Feb 21, 2020 10:58 pm

Reformat mm.dd.yy --> 20yy-mm-dd and move to front

Postby Luuk » Wed Aug 31, 2022 7:06 am

Many apologies for the typo, the title should have said mm.dd.yy --> 20yy-mm-dd, so now I can find it easier.
Luuk
 
Posts: 706
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to reformat existing date in a filename?

Postby Panchdara » Thu Sep 01, 2022 7:06 am

Luuk, that is some RegEx.... where can one find out about the $X directive? I tried in RegEx Buddy and it isn't happy ????

Thanks!
Panchdara
 
Posts: 67
Joined: Sat Jan 09, 2016 7:25 pm

(?X) says next-match or next-replace in RegEx(1)

Postby Luuk » Thu Sep 01, 2022 8:54 am

Greetings Pancdara.. The (?X) is not really regex, its just how bru likes to say "next match" or "next replace"...
Match--1(?X)Match--2(?X)Match--3(?X)...
Replace1(?X)Replace2(?X)Replace3(?X)...

So its really just 3-different regexs, except the 3rd-regex is just deleting, so the 3rd-match doesnt have any replace.
Luuk
 
Posts: 706
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to reformat existing date in a filename?

Postby Panchdara » Wed Sep 07, 2022 7:45 am

Thank you Luuk. I'll have a peek at that... probably way too deep for my understanding, but I'll try it. ????????
Panchdara
 
Posts: 67
Joined: Sat Jan 09, 2016 7:25 pm

Re: How to reformat existing date in a filename?

Postby Panchdara » Wed Sep 07, 2022 7:48 am

Panchdara wrote:Thank you Luuk. I'll have a peek at that... probably way too deep for my understanding, but I'll try it. ????????


"????????" Supposed to be 2 thumb ups, but got lost in translation....
Panchdara
 
Posts: 67
Joined: Sat Jan 09, 2016 7:25 pm

Reformat mm.dd.yy --> 20yy-mm-dd and move to front

Postby Luuk » Fri Sep 09, 2022 10:50 am

Greetings Panchdara.
Its unfortunate, but this regex is probably not a good example for studying, so this is trying to provide some descriptions for...
(?:(?<=^)|(?<=[- ]))(\d\d)\.(\d\d)\.([012]\d)(?=[- ]|$)(?X)(.*)(?<![- ])[- ]*:(20\d\d-\d\d-\d\d)[- ]*(?X)( +$| (?= ))
:20$3-$1-$2(?X)$2 $1 (?X)
Its 3-different Match/Replaces inbetween (?X) and with look-arounds that conduct in this order...


Match/Replace-1 converts mm.dd.yy ===> :20yy-mm-dd
(?:(?<=^)|(?<=[- ]))(\d\d)\.(\d\d)\.([012]\d)(?=[- ]|$)
:20$3-$1-$2
The 1st-two look-behinds are saying that to the left of \d\d should be either 'the-very-beginning' or '-' or 'space'.
The look-ahead at the end is saying that to the right of [012]\d should be either '-' or 'space' or 'at-the-very-end'.
Look-behinds use < to point left, but they forbid using "|" unless having exact same number of characters on both sides.

A look-behind like (?<=abc|def) does conduct properly, but (?<=abc|de) presents 'Regular Expression RegEx(1) is invalid' !!
So this why Im making 1-large group at the very beginning, to settle the 2 look-behinds inside, separated by the '|' character.
Its would have been much easier just using \b instead of look-arounds, but Im only trying to match names like in the samples.

Im only putting ?: inside of this 1st-group, just so that it doesnt waste any group-numbers, because Im wanting to use $1 for (\d\d).
The look-aheads dont care about how many characters are on each side of the '|', so this why the ending look-ahead is just 1-group.


Match/Replace-2 looks for :20yy-mm-dd, moves the date to the front, and replaces its bordering hyphens or spaces with 1-space.
(.*)(?<![- ])[- ]*:(20\d\d-\d\d-\d\d)[- ]*
$2 $1\x20
The look-behind is just looking at (.*) to make sure that his last-character will not be a 'hyphen' or 'space'.
This way, all of the bordering hyphens and spaces will get matched by [- ]* and can be replaced with 1-space.
The \x20 is just saying 'space', because the forum software will not grant any lines ending with spaces.


Match/Replace-3 deletes any extra spaces that Replace-2 might insert, like when there wasnt any $1 to be matched.
( +$| (?= ))
So it deletes 1-or-more spaces at the end, or 1-space when the look-ahead sees another space to its right.
Really you could just leave this part out, with using Remove(5) with checkmarks inside for "D/S" and "Trim".
But usually Im only trying to conduct files like in the samples, so this Match-3 was an exception.


==================================================================================================


If wanting to be very, very careful about never editing anything, unless it was already edited your very first regex...
You can put an illegal character like ':' inside of the Replace-1, then just make sure that your other matches look for it.
So when all of your Match/Replaces is finished, then just add a final match to remove all of the illegal characters.

So like if someone wanted to keep their extra spaces, I would instead conduct it more like...
(.*)(?<![- ])[- ]*(?:(?<=^)|(?<=[- ]))(\d\d)\.(\d\d)\.([012]\d)(?=[- ]|$)[- ]*(?X) +:(?!$)(?X) +:
20$4-$2-$3 $1 :(?X) (?X)
Its still 3-different Match/Replaces inbetween (?X) and with look-arounds that conduct in this order...


Match/Replace-1 converts mm.dd.yy ===> 20yy-mm-dd and moves it to the front.
(.*)(?<![- ])[- ]*(?:(?<=^)|(?<=[- ]))(\d\d)\.(\d\d)\.([012]\d)(?=[- ]|$)[- ]*
20$4-$2-$3 $1 :
This one is a combination of the other Match/Replace-1 and Match/Replace-2, so its "Replace" just adds ' :' to the end.
This way, any future Match/Replaces will look for : so they can only edit text that was first edited by Match/Replace-1.

In the other example, the Match/Replace-1 inserted : and then Match/Replace-2 looks for it, but also removes it.
So everything after that could possibly edit other names, if they had double-spaces or a space at the very end.

Its still always possible that $1 might not exist, and this would also still invent SpaceSpace: after the date.
And if a filename doesnt have any text after the date, then it could be invented at the end of a filename.


Match/Replace-2 converts 1-or-more spaces: ===> 1-space.
\x20+:(?!$)
\x20
The forum software is also prejudice against lines that begin with a space, so just remember that '\x20' == 'space' in regex.
The look-ahead says to not match at the end of filenames, because those should be deleted, instead of converted into 1-space.


Match/Replace-3 deletes 1-or-more spaces: at the end of filenames.
\x20+:
When Match/Replace-2 finishes, the only remaining 1-or-more spaces: would have to be at the end of filenames, so this deletes them.
These last two Match/Replaces could never conduct too many files, because they're always looking for : inserted by Match/Replace-1.


If using Remove(5) with "D/S" and "Trim", then you would only need Match/Replace-1 without any (?X), and just remove ':' from the replace.
Im really was just trying to present how adding an illegal character, can help to let another Match/Replace continue at the same place.
So this way, you never have to depend on using other settings, that might can change too many other filenames.

==================================================================================================

The number of (?X) can also be very important, because if using something like...
Match--1(?X)Match--2(?X)Match--3(?X)Match--4(?X)Match--5
Replace1(?X)Replace2

The Match/Replace--1 and Match/Replace--2 will conduct properly, but Match--3 thru Match--5 would all use Replace2 !!
If you add one (?X) after Replace2, then Match-3 thru Match-5 would all be deleted, because not having their own replace.
This why both of my long-regexs end with (?X) because otherwise their last-match gets replaced, instead of being removed.

A good way to experiment, is to just type in the very first Match/Replace-1, without any renaming.
Then looking at the new-name column to find some clues how the next Match/Replace-2 should conduct.
For myself, Im often use another application to change the text size and color, so its much easier to read.
Luuk
 
Posts: 706
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to reformat existing date in a filename?

Postby AmCdS » Fri Oct 14, 2022 8:07 am

Hello,
I have several files that look like this:

01-10-2022--Malaysia--Super-LeagueKedah-Darul-Aman-FC-0-1-Petaling-Jaya-City20221010-12329-qr5lgk.xml

1) I wanted to change the date format so that it would look like this:

2022-10-01_Malaysia--Super-LeagueKedah-Darul-Aman-FC-0-1-Petaling-Jaya-City20221010-12329-qr5lgk.xml

2) I would also like to remove the ending timestamp and the subsequent code (keeping the file extension):

2022-10-01_Malaysia--Super-LeagueKedah-Darul-Aman-FC-0-1-Petaling-Jaya-City

Can you help me out?
Thank you very much
AmCdS
 
Posts: 4
Joined: Fri Oct 14, 2022 7:58 am

Reverse month/day positions ??

Postby Luuk » Fri Oct 14, 2022 6:23 pm

@ AmCdS
What is the date-formats inside of the 1-example??... It starts like 01-10-2022, but with a timestamp of 20221010.
If the last timestamp is always yyyymmdd, then this could be used to fix any bad date-prefixes like in the example.
Luuk
 
Posts: 706
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to reformat existing date in a filename?

Postby AmCdS » Sun Oct 16, 2022 9:11 am

Thank you for the quick reply @Luuk.

I apologize for not being clear enough with the example.

The first date is the one I want to keep, since it represents the actual date when a given match occurs.

The last date (probably I mistakenly called it timestamp) represents the download date and time, followed by a download code. All of this I wished to erase from my files.

Thank you once again for your time and availability.
AmCdS
 
Posts: 4
Joined: Fri Oct 14, 2022 7:58 am

Re: How to reformat existing date in a filename?

Postby AmCdS » Sun Oct 16, 2022 9:21 am

@Luuk

Just to bring more clarity to another issue I run into.

I mentioned the last date as a timestamp because it always starts with the year (2022 in that case, and it will continue to 2023 onwards).

And I cannot use any change from digit to non-digit for example, because there are files like this:

10-09-2022--Switzerland--YAPEAL-Promotion-LeagueZurich-II-5-0-FC-Baden-189720221016-77748-eb4qe5

Where the second team is named FC-Baden-1897... the part I need to remove is "20221016-77748-eb4qe5".
AmCdS
 
Posts: 4
Joined: Fri Oct 14, 2022 7:58 am

Reverse month/day positions in date-prefix

Postby Luuk » Sun Oct 16, 2022 7:16 pm

@ AmCdS
To reverse month/day positions, move yyyy to front, and remove those download-codes, RegEx(1) can use a "Match" and "Replace" like...

^(\d\d)(-\d\d-)(202\d)--(.+)202\d{5}-\d{5}-[^-]{6}$
\3\2\1_\4
Luuk
 
Posts: 706
Joined: Fri Feb 21, 2020 10:58 pm

Re: How to reformat existing date in a filename?

Postby AmCdS » Sun Oct 16, 2022 10:54 pm

Thank you very much for your help @Luuk!!!
AmCdS
 
Posts: 4
Joined: Fri Oct 14, 2022 7:58 am


Return to How-To