Convert date from mm-dd-yy to yyyy-mm-dd in file name

A swapping-ground for Regular Expression syntax

Convert date from mm-dd-yy to yyyy-mm-dd in file name

Postby DaveApple1 » Wed Jan 14, 2015 4:51 pm

Good Day. I have hundreds of different files all with the date format of mm-dd-yy which I need to change to yyyy-mm-dd. The hard part is not all of the files have the date at the end of the file name. I have found a few example that partly cover this but none have worked correctly for me. Any help would be greatly appreciated.

Example:
file_04-14-12.pdf
file_04-14-12-signed.pdf
file_01-01-05.pdf
DaveApple1
 
Posts: 2
Joined: Sat Dec 03, 2011 5:51 pm

Rearrange swap date timestamp month day year

Postby Stefan » Wed Jan 14, 2015 5:15 pm

DaveApple1 wrote:Good Day. I have hundreds of different files all with the date format of mm-dd-yy which I need to change to yyyy-mm-dd. The hard part is not all of the files have the date at the end of the file name. I have found a few example that partly cover this but none have worked correctly for me. Any help would be greatly appreciated.

Example:
file_04-14-12.pdf
file_04-14-12-signed.pdf
file_01-01-05.pdf



Try

FROM:
file_04-14-12.pdf
file_06-23-12-signed.pdf
file_01-09-05.pdf

TO:
file_12-14-04.pdf
file_12-23-06-signed.pdf
file_05-09-01.pdf

RULE:
Convert date from mm-dd-yy to yyyy-mm-dd in file name


USE
RegEx(1)
Match:(.+_)(\d\d)(-\d\d-)(\d\d)(.*)?
Repla:\1\4\3\2\5


Add an additional rule:
Repl.(3)
Replace:\5
With:
to remove the redundant " \5"
(read http://www.bulkrenameutility.co.uk/forum/viewtopic.php?p=6272#p6272 for more)



 
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Convert date from mm-dd-yy to yyyy-mm-dd in file name

Postby gordobru » Sat Mar 21, 2015 5:00 pm

Hello, I am rather new to BRU, but I absolutely love it.
I tried using the expression detailed below, however, this converts to yy-dd-mm
It does NOT convert to yy-mm-dd and of course, not to yyyy-mm-dd

I too have been looking for the appropriate expression to change thousands of files on my server.
Through trial and error, I used the following:
RegEx (1)
Match: (.*)(\d\d)(-\d\d-)(\d\d)(.*)?
Replace: \120\4-\2\3\5

Examples FROM:
Axiom Black Cartridge Receipt 01-09-12.pdf
Calendars packing slip 09-29-11.pdf
Office Max Office Supplies 12-14-11.pdf
Postage Invoice 01-21-11.pdf
Staples shipping--5 red 3 ring binders 4 inch 11-12-11.pdf
OfficeMax Invoice A839266286 05-10-11.pdf
Discount School 2011-08-05 Supply Packing List.pdf

Examples BECOMES:
Axiom Black Cartridge Receipt 2012-01-09-.pdf
Calendars packing slip 2011-09-29-.pdf
Office Max Office Supplies 2011-12-14-.pdf
Postage Invoice 2011-01-21-.pdf
Staples shipping--5 red 3 ring binders 4 inch 2011-11-12-.pdf
OfficeMax Invoice A839266286 2011-05-10-.pdf
Discount School 202005-11-08- Supply Packing List.pdf WRONG of course

I need to get rid of the "-" dash at the end
Cheers,
G
gordobru
 
Posts: 2
Joined: Sat Feb 28, 2015 5:02 am

Convert swap rearrange sort reorder date mm-dd-yy

Postby Stefan » Tue Jun 16, 2015 6:46 am

gordobru wrote:I need to get rid of the "-" dash at the end


No need to have that dash at the end at first place.

Just don't capture the hyphen, match it only:

RegEx (1)
Match: (.*)(\d\d)(-\d\d-)(\d\d)(.*)? <<<< yours
Match: (.*)(\d\d)(-\d\d)-(\d\d)(.*)?
Replace: \120\4-\2\3\5




.
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Convert date from mm-dd-yy to yyyy-mm-dd in file name

Postby Boofer » Mon Aug 01, 2022 3:13 pm

New to this but just found the utility.

The method here works great for files that have leading zero's as the MM

But it is not processing files that do NOT have a leading zero for MM (e.g 8-15.22 opposed to 08-15-22).

What variable can I use for the rule to work with both formats? M and MM ?

Or is there a quick rule to add a leading zero to those names that do not have one for the MM ?

Thanks for the help all. !!!
Boofer
 
Posts: 4
Joined: Mon Aug 01, 2022 3:07 pm

Unknown date-formats

Postby Luuk » Mon Aug 01, 2022 3:58 pm

Greetings Boofer, it depends on your date-formats, but you are only giving one example, without the example new-name?
Yes, its very easy to conduct any or-situations with the regex, but all of these questions could change the answer...
Does "dd" always have two-digits ??
Does "-" always separate "m/mm" from "dd" ??
Does "." always separate "dd" from "yy" ??
Does the new-name always need "20" in front of the year, or sometimes "19" ??
Luuk
 
Posts: 494
Joined: Fri Feb 21, 2020 10:58 pm

Re: Convert date from mm-dd-yy to yyyy-mm-dd in file name

Postby Boofer » Mon Aug 01, 2022 4:27 pm

Thanks so much for the reply, I'm a network nerd that got caught up helping a doctor buddy............

So here is what I think might give more clarification - see if this helps;

OLD NAMES
last, first 10-6-15 Progress Note.docx
last, first 9-15-15 Progress Note.docx
last, first 01-07-15 Progress Note.docx
last, first 03-03-15 Progress Note.doc
last, first 1-07-15 Progress Note.doc

DESIRED NEW NAMES
last, first 2015-10-06 Progress Note.docx
last, first 2015-09-15 Progress Note.docx
last, first 2015-07-15 Progress Note.docx
last, first 2015-03-15 Progress Note.doc
last, first 2015-01-07 Progress Note.doc
Boofer
 
Posts: 4
Joined: Mon Aug 01, 2022 3:07 pm

Re: Convert date from mm-dd-yy to yyyy-mm-dd in file name

Postby Boofer » Mon Aug 01, 2022 4:31 pm

I think I messed up that last example :) Apologies........

End result desired is YYYY-MM-DD

OLD NAMES
last, first 10-6-15 Progress Note.docx
last, first 9-15-15 Progress Note.docx
last, first 01-07-15 Progress Note.docx
last, first 03-03-15 Progress Note.doc
last, first 1-07-15 Progress Note.doc

DESIRED NEW NAMES
last, first 2015-10-06 Progress Note.docx
last, first 2015-09-15 Progress Note.docx
last, first 2015-01-07 Progress Note.docx
last, first 2015-03-03 Progress Note.doc
last, first 2015-01-07 Progress Note.doc
Boofer
 
Posts: 4
Joined: Mon Aug 01, 2022 3:07 pm

(m|mm)-(d|dd)-(yy) ===> 20yy-mm-dd

Postby Luuk » Mon Aug 01, 2022 6:04 pm

With a checkmark inside for "v2", the "Match" and "Replace" can look like...
(?<!\d)([1-9]-\d\d?-\d\d)(?!\d)(?X)(?<!\d)(\d\d-)([1-9]-\d\d)(?!\d)(?X)(?<!\d)(\d\d-\d\d)-(\d\d)(?!\d)
0$1(?X)${1}0$2(?X)20$2-$1

If there can be many dates inside of the filename, add /g to the end of the "Match" to change them all.
This format can help anybody who doesnt always use spaces to separate their dates inside the filenames.
But to only convert dates that are separated by spaces, it could look like...

( )([1-9]-\d\d?-\d\d )(?X)( \d\d-)([1-9]-\d\d )(?X) (\d\d-\d\d)-(\d\d)\x20
${1}0$2(?X)${1}0$2(?X) 20$2-$1\x20

Except Im not sure if this forum will present spaces at the end of a line, so Im just using \x20 to say "space".
Also, if only wanting to conduct .doc and .docx files, then can use a Filters(12) "Mask" like... *.doc *.docx
Luuk
 
Posts: 494
Joined: Fri Feb 21, 2020 10:58 pm

Re: Convert date from mm-dd-yy to yyyy-mm-dd in file name

Postby Boofer » Mon Aug 01, 2022 7:29 pm

Now THAT worked pretty darn cool !!! There were a few exceptions but it was obvious that they were variants from the naming convention. Had about 20 exceptions (no change) with the 600 test files......not too shabby.

Cant tell you how much I appreciate the feedback. I have to admit that I'm still getting my head around those variables.......I thought that programming in ANSI standard MUMPS was complicated......HA !!

Now I will figure out how to move the unchanged (aka "same") files into some kind of "exceptions" folder.........then I can manually review those exceptions.

Cool beans and thank you :)
Boofer
 
Posts: 4
Joined: Mon Aug 01, 2022 3:07 pm

(m|mm)-(d|dd)-(yy|20yy) ===> 20yy-mm-dd

Postby Luuk » Mon Aug 01, 2022 10:29 pm

Those examples would not edit any dates looking like 7-0-12 or 0-10-12 or 1-1-2022, but to also conduct dates ending like 20yy, it could be like...
(?<!\d)([1-9]-\d\d?-(20)?\d\d)(?!\d)(?X)(?<!\d)(\d\d-)([1-9]-(20)?\d\d)(?!\d)(?X)(?<!\d)(\d\d-\d\d)-(\d\d)(?!\d)(?X)(?<!\d)(\d\d-\d\d)-(\d{4})(?!\d)
0$1(?X)${1}0$2(?X)20$2-$1(?X)$2-$1

If the exceptions need more characters like "-" or "_" or "." for the mm/dd/yy separators, then it would be more like...
(?<!\d)([1-9][-._]\d\d?[-._](20)?\d\d)(?!\d)(?X)(?<!\d)(\d\d[-._])([1-9][-._](20)?\d\d)(?!\d)(?X)(?<!\d)(\d\d[-._]\d\d)[-._](\d\d)(?!\d)(?X)(?<!\d)(\d\d[-._]\d\d)[-._](\d{4})(?!\d)
0$1(?X)${1}0$2(?X)20$2-$1(?X)$2-$1

If for some reason, the exceptions can have multiple separators like m--d..yy or m._.d-..-yyyy , then its more like...
(?<!\d)([1-9])[-._]+(\d\d?)[-._]+((20)?\d\d)(?!\d)(?X)(?<!\d)(\d\d)[-._]+([1-9])[-._]+((20)?\d\d)(?!\d)(?X)(?<!\d)(\d\d)[-._]+(\d\d)[-._](\d\d)(?!\d)(?X)(?<!\d)(\d\d)[-._]+(\d\d)[-._]+(\d{4})(?!\d)
0$1-$2-$3(?X)$1-0$2-$3(?X)20$3-$1-$2(?X)$3-$1-$2

These regexs will not edit any dates starting with 4-digits, so its probably safe not having to move the exceptions.
The regular-expressions is not really that hard to code, its just often being very hard to follow, with the eyesight.
Its probably like most other things, it only seems hard when you dont know it, but then after learning, it seems easy!

I will come back later to give better descriptions for what everything is doing, and try to add coloring so its easier to follow.
Luuk
 
Posts: 494
Joined: Fri Feb 21, 2020 10:58 pm

(m|mm)-(d|dd)-(yy|20yy) ===> 20yy-mm-dd

Postby Luuk » Fri Aug 05, 2022 6:47 am

Apologies for not coming back sooner, this can probably help for understanding how it conducts.
The (?X) just says "Next-Match" or "Next-Replace", so the format is like...
Match--1(?X)Match--2(?X)Match--3(?X)...
Replace1(?X)Replace2(?X)Replace3(?X)...


This very long regex does have four different Match/Replaces to convert dates like... (m|mm)-(d|dd)-(yy|20yy) ===> 20yy-mm-dd
(?<!\d)([1-9]-\d\d?-(20)?\d\d)(?!\d)(?X)(?<!\d)(\d\d-)([1-9]-(20)?\d\d)(?!\d)(?X)(?<!\d)(\d\d-\d\d)-(\d\d)(?!\d)(?X)(?<!\d)(\d\d-\d\d)-(\d{4})(?!\d)
0$1(?X)${1}0$2(?X)20$2-$1(?X)$2-$1
But if removing all of the (?X), its just 4 different "Match" and "Replaces" to conduct in this exact order...

(?<!\d)([1-9]-\d\d?-(20)?\d\d)(?!\d)
0$1
The red-parts is call negative-lookarounds, and they do just say what not to match, the < says look-left, but otherwise they look-right.
The lookarounds only match conditions, without consuming any characters, so $1 wont reference anything matched by a lookaround.
The (?<!\d) makes sure there is no digit to the left of [1-9], and (?!\d) makes sure there is no digit to the right of (20)?\d\d.

The ? is a shortcut to say "either 0-or-1", so \d\d? says 1-or-2 digits, and (20)?\d\d says either 20\d\d or \d\d.
The 0$1 prefixes zero to the month [1-9], if its followed by either day-format, and then either year-format.
So now that we know all of the months have been fixed into two-digits, the next regex to conduct days...


(?<!\d)(\d\d-)([1-9]-(20)?\d\d)(?!\d)
${1}0$2
This to match any dates starting with 2-digits, followed by[1-9] as the day, followed by either year-format.
Its unfortunate, but $10 says GroupTen, so we use ${1}0 to say Group1Zero to prefix the 1-digit days.
So now that months and days have been fixed into two-digits, the next regex to convert yy ===> 20yy.


(?<!\d)(\d\d-\d\d)-(\d\d)(?!\d)
20$2-$1
This just matches any dates like mm-dd-yy, then it prefixes 20 before yy, and puts 20yy to the front.


(?<!\d)(\d\d-\d\d)-(\d{4})(?!\d)
$2-$1
This last one matches any "dates" like mm-dd-yyyy, and then it just puts the "yyyy" to the front.
Except Im being lazy about the years, because it could change... 01-22-9921 ===> 9921-01-22!
So to make this more exact, you can just change... \d{4} ===> 20\d\d


The other examples was just using [-._] to grant more separators, instead of being strict with the "-" character.
So its really not complicated once the formats is understood, but for me, its often hard to follow with the eyesight.
Sometimes I do even use a text-editor, just so that the size and coloring will makes it easier to follow.
Luuk
 
Posts: 494
Joined: Fri Feb 21, 2020 10:58 pm


Return to Regular Expressions