How to rename file based on today's date?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

How to rename file based on today's date? Sun Jul 01, 2012 1:40 pm • by Bonanzan
I purchased Hazel a couple of weeks ago and I'm loving it!

I'm obviously still in the learning stage and am having difficulty figuring out how to rename a file based on today's date, rather than the date added, creation date or modification date.

Here's the scenario:

At the end of each month we generate a report for our company's monthly sales. This report is generated manually and the date that it's created could be at the end of the current month, or the beginning of the following month. The date could be the last day of the month, which is logical. But it could also be a few days into the following month if, for example, the month ends on a weekend and the report isn't run until the following week. The file that is generated is simply titled "Monthly Sales" and I would like to have hazel change that to read "2012-06 Monthly Sales", if it was June's sales report.

To deal with the fact that the file's creation date could be in either the month that the report is for, or the month after the report is for, my idea was to have hazel wait until the file was a few days old and then rename it based on the current date minus 1 month. Similar to how you can do that with date added minus 1 month. However I can't seem to find a way to rename based on the current date instead of a date contained in the file metadata.

It seems as though it should be simple, so I'm guessing that I'm just missing something. If anyone could point me in the right direction I would appreciate it.

If Hazel can't do this, I'm open for any suggestions of a workaround.

Thanks, in advance, for your help.
Bonanzan
 
Posts: 10
Joined: Sun Jul 01, 2012 1:02 pm

Current date isn't in the list of tokens but it's in the feature database for future inclusion. The only real workaround I can think of is to have an AppleScript export it as a token. Look up applescript and export tokens in the help for more info.
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

if you do use applescript to generate the date token, you might as well have it add a couple days and subtract one month in one fell swoop, rather than having Hazel leave the file un-labeled for several days.
ecormany
 
Posts: 28
Joined: Wed Apr 25, 2012 6:15 pm

Thanks for the responses Mr_Noodle and ecormany.

Actually I not well versed in Applescript and as yet have not figured out how to incorporate this into the Hazel rules. If either of you could assist me I would greatly appreciate it. If it's too complicated to explain here, perhaps you could point me in the right direction?

Thanks!
Bonanzan
 
Posts: 10
Joined: Sun Jul 01, 2012 1:02 pm

Re: How to rename file based on today's date? Tue Jul 10, 2012 11:11 pm • by a_freyer
I have cross-posted this method in Tips and Tricks

Try this:

Code: Select all
if (all) of the following conditions are met for the (the file or folder being matched)
      <<Whatever you want>>

Do the following to the matched file or folder:
     Run Applescript (embedded script)
     Rename with pattern ... (comment: leave empty for now)


Once you've done this, the embedded script is this:
Code: Select all
return {hazelExportTokens:{ThisMonth:do shell script "date +%Y-%m", LastMonth:do shell script "date -v -1m +%Y-%m"}}



Next, after you've pasted the AppleScript, click the (i) button in the upper right hand corner. In the window that pops up, click [+] and type ThisMonth, click [+] again and type LastMonth

Now, in your rename with pattern action you'll have a "ThisMonth" and a "LastMonth" token. Drag either up to the input box, add "-Monthly Sales" or whatever you'd like, and you're set to go:


Code: Select all
if (all) of the following conditions are met for the (the file or folder being matched)
      <<Whatever you want>>

Do the following to the matched file or folder:
     Run Applescript (embedded script)
     Rename with pattern (ThisMonth •) - Monthly Sales


Now, your files can be renamed:

2012-07 (ThisMonth)
2012-06 (LastMonth)

If you want to format the date any other way, then change the formatting specifiers in the "do shell script" blocks of the applescript based on the formatting guidance you'll find here.
Last edited by a_freyer on Wed Jul 11, 2012 2:48 pm, edited 1 time in total.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Thank you a_freyer for your terrific response.

Your instructions are written so clearly that even a novice like me had no trouble following them. And that's quite an accomplishment! :D

I created a rule following your instructions and it worked perfectly the first time. Many thanks for taking the time to help.

ecormany's post sounds like a good idea:

ecormany wrote:if you do use applescript to generate the date token, you might as well have it add a couple days and subtract one month in one fell swoop, rather than having Hazel leave the file un-labeled for several days.


Is there an easy way to implement this? Or, am I better off with my original idea of having Hazel wait a few days before running the rule? It seems as though either way should work out correctly now that the current date problem has been solved.
Bonanzan
 
Posts: 10
Joined: Sun Jul 01, 2012 1:02 pm

Bonanzan wrote:
ecormany's post sounds like a good idea:

ecormany wrote:if you do use applescript to generate the date token, you might as well have it add a couple days and subtract one month in one fell swoop, rather than having Hazel leave the file un-labeled for several days.


Is there an easy way to implement this? Or, am I better off with my original idea of having Hazel wait a few days before running the rule? It seems as though either way should work out correctly now that the current date problem has been solved.


Yup! I've added a much larger hint in the Tips and Tricks Forum because I assumed that this would be helpful if further explained.

What you can do is replace the "date +%Y-%m" with this:

#Add Three Days, Subtract 1 Month
date -v +3d -v -1m +%Y-%m

#Add Ten Days, Subtract 1 Month
date -v +10d -v -1m +%Y-%m

#BEST OPTION: This will only subtract a month if the current date is within the first 7 days of the month.
date -v -$(test $(date +%d) -lt 7 && echo 1 || echo 0)m +%Y-%m

With this final option, the result is like this:

If the report is run in late July, the report will be dated for July.
If the report is run in the first week of August, the report will be dated for July.

Basically, the "reporting month" is now seven days behind the actual month. Ostensibly, this has a similar effect to the "add a few days, subtract a month" without the potential issue of running the report on, say, the 24th of December for the Month of December. In the case of "add a few days, subtract a month" you might actually produce a report for November.


Hope these are helpful too!
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

I can only say one thing about a_freyer . . . "You Rock!"

I love your suggested script that tests for the date. It solves the problem in a very clean way. I incorporated it into your earlier script in order to make one script that I can use in various ways. It works great!

Here's what I came up with:

Code: Select all
return {hazelExportTokens:{LastMonthWithin7Days:do shell script "date -v -$(test $(date +%d) -lt 7 && echo 1 || echo 0)m +%Y-%m", ThisMonth:do shell script "date +%Y-%m", LastMonth:do shell script "date -v -1m +%Y-%m"}}


Yesterday I didn't understand Applescript at all, but now I'm beginning to see the light. I purchased Hazel after reading up on it because I could see that it was a powerful tool that I could put to good use. And now I can see that if you've got the knowledge to be able to combine the great features of Hazel with custom Applescripts, the possibilities are even more amazing. The combination of the two takes Hazel to a whole new level. I guess I'm going to have to add Applescript to the long list of things I've got to find time to learn. There are just not enough hours in the day. -- If anyone has any suggestions for expanding time... I'd love to hear them! Or perhaps cloning?

In the meantime... Is it possible to do similar calculations in applescript based on a file's creation or modification date? I know Hazel has tokens that make it easy to do basic math with the dates, but I was wondering more about the kind of testing formula that is used in the "LastMonthWithin7Days" script. I'm thinking it might be a great way to handle files if you were batch processing a folder of old files.
Bonanzan
 
Posts: 10
Joined: Sun Jul 01, 2012 1:02 pm

I can write a bit more on this later, but the general idea is to use the stat command to retrieve modified or creation dates of files, then modify the date as needed from there:

Code: Select all
stat -f %Sm /Path/To/The/File.txt


Will output the modified date of the file. If you want creation date, use %Sc (although technically, this is the date permissions on the file were last changed, usually only on creation date.).
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Thanks for the info. I'll play around with it a bit and see if I can come up with something useful.
Bonanzan
 
Posts: 10
Joined: Sun Jul 01, 2012 1:02 pm

Re: How to rename file based on today's date? Sun May 21, 2017 12:45 am • by MamZelle
Hello

Reading this post got me thinking...

I have a .text file that I get every week which is my call log from my phone. I've set up a rule that creates a "beginning date" and an "end date" token from the first and last entry in my file in order to rename it like this... "beginning date" to "ending date".txt

My problem is that my first call of the week doesn't necessarily come in on Sunday and my last call on Saturday ... I would like my "beginning date" to be the Sunday of said week as well as my "ending date" to be the Saturday of the same week (i.e., 2017-05-14 to 2017-05-20). I've tried with the "adjust date" feature but it's not the solution

The script in this post gives me hope but I don't know the fist think about scripting ... So I'm lost !!!

Anyone could help?
MamZelle
 
Posts: 4
Joined: Sat May 13, 2017 11:54 am
Location: Longueuil, Qc. Canada


Return to Support

cron