Only rename files that have a date in the middle

Bulk Rename Utility How-To's

Only rename files that have a date in the middle

Postby StraightShooter » Sat Mar 21, 2020 12:04 pm

I have many hundreds, if not thousands of files with names like "somethinghere_04_28_2003_somthingelse" but where i would whether have them like "2003_04_28_somethinghere--somthingelse" but if the numbers in the middle are not a date, do not rename them. For example, files with names like "somethinghere_04_2003_somthingelse" or "somethinghere_2003_somthingelse" should not be changed! How to do them all at the same time without messing up the files that i want to stay the same? Thanks much in advance.
StraightShooter
 
Posts: 5
Joined: Sat Mar 21, 2020 9:27 am

Re: Only rename files that have a date in the middle

Postby therube » Sat Mar 21, 2020 9:03 pm

A start...

_\d\d_\d\d_\d\d\d\d\_ == _2-digits (Mo)_2-digits (Da)_4-digits (Yr)_

You could put in tighter rules...

_[01]\d, only 12 months, so the first digit of the month has to be 0 or 1

_[0123]\d, only a maximum of 31 days, so the first digit of the day can only be 0 or 1 or 2 or 3

_20\d\d, depending on your range of possible years, maybe years starting with 20, so 2000 to 2099, would suffice


So it could be that something general like:
_\d\d_\d\d_\d\d\d\d\_ would work for you
or you might need something like:
_[01]\d_[0123]\d_20\d\d for further tightening

Both of the above would find: _03_21_2020_


---


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

Code: Select all
somethinghere_04_28_2003_somthingelse -> _04_28_2003_somethinghere--somthingelse


Now that doesn't deal the the opening _, nor does it put your date in the correct order, but you can deal with that part afterwards - or you could change your regex: to deal with it initially.

Lets leave it relatively simple for now.
You can test with a basic matching regex: to see that your "date" is working as expected.
Code: Select all
Match:  (_\d\d_\d\d_\d\d\d\d\_)
Replace:  \1

Every matched file will display only the date part.
(Obviously you do not want to rename - at this point. It's just to help make it more obvious just what files would be affected, that no non-wanted files happen to end up being matched.)
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Only rename files that have a date in the middle

Postby StraightShooter » Sun Mar 22, 2020 8:06 am

Thanks therube for your reply! and here is what I try
Match: (.*)(_\d\d_\d\d_\d\d\d\d\_)(.*)
Replace: \2\1--\3
It is very close, but like you say it doesnt make the dates in the right order with the year in front. So if i first use this to rename some of the files, what is the next step to make them like "2003_04_28_somethinghere--somethingelse" insted of "_04_28_2003_somethinghere--somethingelse". I want to test first with only a few files before i try to rename lots of them because i have so very many files. Thanks much in advance!!
StraightShooter
 
Posts: 5
Joined: Sat Mar 21, 2020 9:27 am

Re: Only rename files that have a date in the middle

Postby therube » Sun Mar 22, 2020 3:07 pm

So now we have to split the "date" into two parts; Mo+Da & Yr.

Code: Select all
(_\d\d_\d\d_\d\d\d\d\_)
to
_(\d\d_\d\d)_(\d\d\d\d)_
   Mo   Da      Yr

Similarly, we ignore the preceding & ending _, adding them back later (in the Replace:) if needed.

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

Code: Select all
somethinghere_04_28_2003_somthingelse -> 2003_04_28_somethinghere--somthingelse
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Only rename files that have a date in the middle

Postby StraightShooter » Sun Mar 22, 2020 4:08 pm

Thanks again, here is what i am doing for the step 2 to get the year fixed.
Match: (.*?)_(\d\d_\d\d)_(\d\d\d\d)_(.*)
Replace: \3_\2_\1\4
I have to remove the - out because if i dont i get two whole differnt sets of them. It is working great so far i am only doing about 50 at a time in step 1 and then i do step 2, and holding my breath each time lol, but it is working great. Im not brave enough to do them all at the same time lol. I know there is undo but for now i feel safer looking them over. Thank you very very VERY much!!!!!!
StraightShooter
 
Posts: 5
Joined: Sat Mar 21, 2020 9:27 am


Return to How-To