Append File Title with Date

I have a folder full of text files with titles in this format:

 <Title> - <DayName>, <MonthName> <DayNum>, <YearNum> at <Time>

example:

Street Cuisine - Wed, Jan 5, 2022 at 9-42 AM

I want to append to the beginning of the title:

<YearNum>-<MonthNum>-<DayNum> 

So the final title would be:

2022-1-5 Street Cuisine - Wed, Jan 5, 2022 at 9-42 AM

The methods I've come up with seem pretty primitive, like using Switch actions to figure out the date.

Is there a more elegant way to do this? Maybe with regex?

Thanks in advance for your suggestions

Were these files created on the same dates as their titles? If they were, you could use a Get File Attribute action to obtain each file's creation date and use that in conjunction with the ICUDateTimeFor token to append the dates more reliably. If the dates in the file names and the files' creation dates have nothing to do with each other, then yes, a regex combined with a Switch/Case or similar action to convert the ordinal month into a numerical one is probably the way to go.

1 Like

Thanks for the help.

Most of the files were created on the title date, but some of them were created the next day -- so their title date is one day earlier than their creation date.

My first thought was to use the creation date, compare it to the title date and then adjust the ones that mismatch. However, wouldn't it be simpler/easier to just convert all the title dates with Regex/Switch?

It may, but only if the date in every file name is in exactly the same format you listed in your example—the three letter ordinal month followed by a space, a digit, a comma, another space, and the four-digit year. If there are exceptions to that pattern, such as the day coming before the month or the year coming before the day, using the creation date and adjusting any file names that are off by a day may still be faster and easier.

If you are sure that all the file names follow that format, then this regex should get you started:

(\w{3}) (\d{1,2}), (\d{4})

You can use that in a Search using Regular Expression action to capture all three parts of the date into variables like so:

Then use a Switch to change the contents of the LocalMonth variable into its numeric form, after which you can combine the variables into the prefix format you want, i.e. %LocalYear%-%LocalMonth%-%LocalDay%.

2 Likes

This is very helpful.

The dates were generated automatically, so they are perfectly consistent.

The only variance in the date format is that there is no padding in the day number. So 1-9 are single digits and 10-31 are doubles. But it looks like you covered that already in your regex.

I'll give this a try -- again, many thanks.

2 Likes

I strongly urge you to pad those day and month numbers to 2 digits -- 2022-01-05 -- because then the ASCII sort order will be the same as the date order.

You may never need to sort them - but if you do that'll be a huge time saver.

1 Like