Page 1 of 1

Use Any Date/Time as Custom Token for Renaming/Sorting Files

PostPosted: Tue Jul 10, 2012 11:37 pm
by a_freyer
I originally posted this as a reply to a support request, but I think there are other folks who would find it handy.

This allows you to use any date or time as a custom token for file/folder naming ... regardless of a file's metadata.

This is exceptionally helpful if you want to:

  • Rename files in your downloads folder with the date that they were downloaded.
  • Rename class notes / meeting minutes with the current date or meeting time
  • Sort files into folders based on when they were scanned
  • ... whatever else you'd like ...

Perhaps you want to rename/sort a file with these tokens:
  • Today's Date
  • Time Right Now
  • Last Month's Date
  • Ten Minutes Ago
  • Name of the Current Day of the Week
  • Name of the Current Month
  • ... whatever else you want relating to date or time ...

Step 1: add your rule with an embedded AppleScript action:


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)



Step 2: copy the AppleScript into Hazel:


Code: Select all
return {hazelExportTokens:{CustomDateTokenNameGoesHere:do shell script "date +%Y-%m"}}


The section date +%Y-%m is where the date format magic happens. Right now, this example returns 2012-06 for the year and month.

Here are some other examples you might want to use:

# Year and Month (e.g. 2012-07)
date +%Y-%m

# Year and 1 Month Later (notice the adjust command -v precedes the +1m which is one month forward)
date -v +1m +%Y-%m

# Year and 1 Month Back (notice the adjust command -v precedes the -1m which is one month back)
date -v -1m +%Y-%m

#Current TIme (e.g. 12:01:00)
date +%H:%M:%S

#One Hour From Now
date -v +1H +%H:%M:%S

#Current Day of the Week (e.g. Monday)
date +%A

#Current Day of the Week Abbreviation (e.g. Mon)
date +%a

#Current Month (e.g. July)
date +%B

#Subtract 1 Month ONLY if the Current Date is within the First 7 Days of the Month (e.g. 2012-06 is output until 07/07/2012, and then 2012-07 is output.
date -v -$(test $(date +%d) -lt 7 && echo 1 || echo 0)m +%Y-%m


And so on. You can see all of the date format options you have here.

Step 3: Make sure to add your custom token name to the token list!

Before you click out of the embedded script window, click the (i) in the upper right. Now, click the [+] in the lower left and type the exact name that appears in your custom script. In my example, the name is CustomDateTokenNameGoesHere but you can name it whatever you want.

Step 4: Add More Time Tokens if you want

By adding additional comma-delimited tokens to the applescript, and adding the names in the token naming window (step 3) you can create any number of time tokens:

Code: Select all
return {hazelExportTokens:{CustomDateTokenNameGoesHere:do shell script "date +%Y-%m",AnotherCustomDateTokenNameGoesHere:do shell script "date -v -1Y +%Y-%m"}}



Hope someone finds this useful!

Re: Use Any Date/Time as Custom Token for Renaming/Sorting F

PostPosted: Sun Dec 15, 2013 2:21 am
by kshivner
I would like to take the year or any word in the file name and create a folder with that value

Example 2014-01 - yak bill.pdf

I want to extract 2014 and put the file in a folder with that name and if it does not exist create it

I tried the video on this but instead of using just the year it added the current month and day

Thanks

Keith

Re: Use Any Date/Time as Custom Token for Renaming/Sorting F

PostPosted: Mon Dec 16, 2013 12:25 pm
by Mr_Noodle
Please post a new topic in the Support forum as you don't need a script to do this so it isn't relevant to this thread.

Re: Use Any Date/Time as Custom Token for Renaming/Sorting F

PostPosted: Mon Dec 16, 2013 2:38 pm
by kshivner
I figured it out and got it working

Thanks

Re: Use Any Date/Time as Custom Token for Renaming/Sorting F

PostPosted: Tue Dec 31, 2013 6:47 am
by pbain
How did you do this? I would like to use the same rule to sort photos by the date they are created. If a folder does not exist for the date, I would like the rule to create a folder and then move the photo to the new folder.

Re: Use Any Date/Time as Custom Token for Renaming/Sorting F

PostPosted: Thu Jan 02, 2014 4:22 pm
by Mr_Noodle
Again, please post a new topic in the support forum with the specifics of your case.

Re: Use Any Date/Time as Custom Token for Renaming/Sorting F

PostPosted: Fri Jan 23, 2015 6:45 pm
by rbianchi
Sorry if I ask for basic questions, but I am newby with Hazel.
I want to rename a bill that in the file name has the date in the format 'Dec_2014' and I want to cenvert to the format '2014.12'
How to do that? It is possible?
Many thanks in advance!

Re: Use Any Date/Time as Custom Token for Renaming/Sorting F

PostPosted: Mon Jan 26, 2015 2:07 pm
by Mr_Noodle
Please post in the support forum as a separate question. This does not below in this topic or in this forum.