Adding A Number To A File Name Based On Creation Date & Time

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

Moderator: Mr_Noodle

I'm stuck with a rule I want to execute, any help would be greatly appreciated!

I import photos from my camera and want to have Hazel rename them with the date created, then a number starting at 001 and ascending from there based on the creation date from oldest to newest. I also want the number to restart at 001 each new date. For example, if I have these photos taken at these times:

ABC taken 3/25/12 1pm
DEF taken 3/25/12 1:15pm
GHI taken 3/25/12 1:30pm
JKL taken 3/30/12 2pm
MNO taken 3/30/12 2:30pm
PQR taken 3/30/12 3pm

I want them to be renamed as:
120325_001 ABC
120325_002 DEF
120325_003 GHI
120330_001 JKL
120330_002 MNO
120330_003 PQR

Currently I am achieving this by having Hazel rename the files without the number, as 120320_ ABC and then sort into subfolders by day, and then I run those subfolders through A Better Finder Rename. But I'd like to do it all in Hazel.

I tried making a rule that says Rename > with pattern > date created_# name extension but the numbers I get seem to be arbitrary. I end up with:
120325_065 ABC
120325_066 DEF
120325_072 GHI
120330_059 JKL
120330_060 MNO
120330_061 PQR
abbimj
 
Posts: 5
Joined: Fri Mar 30, 2012 12:46 pm

This is an interesting problem - I'll investigate this further and see what can be done. The sequential numbering is likely "random" because of two reasons: (1) How Hazel sorts the files before sequential numbering and (2) because Hazel is sequentially numbering the entire directory.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Ok, I've found a solution. It's lengthy, but it works. The hazel rule file is here:

http://dl.dropbox.com/u/23950/Hazel%20Rules/Hazel%20Testing.hazelrules

Seven Rules (in order):

(1) Ignore Non-Pictures
(2) Ignore Good Format
(3) Add Date Created Prefix
(4) Sort into Creation Date Subfolder & Append Sequence
(5) Delete Empty Folders
(6) Run on Folder Content
(7) Move to Parent if Good Format

So, lets tackle these one at a time. Sounds like you're pretty familiar with Hazel, so I'll just type the content of the rule. If you have questions about why or how, let me know.

Code: Select all
Ignore Non-Pictures

if (all) of the following conditions are met for (current file or folder)
    Kind IS NOT image
    Kind IS NOT folder

Then (ignore)


Code: Select all
Ignore Good Format

if (all) of the following conditions are met for (current file or folder)
   Kind IS NOT folder
   Name MATCHES (•DATE)_(123) (...)
   Subfolder depth IS 0

Then (ignore)


Code: Select all
Add Date Created Prefix

if (all) of the following conditions are met for (current file or folder)
    Name DOES NOT MATCH (•Date Created)_(...)
    Kind IS NOT folder
    Subfolder depth is 0

Then
    Rename WITH (date created)_(name)(extension)


Code: Select all
Sort into Creation Date Subfolders & Sequentialize

if (all) of the following conditions are met for (current file or folder)
    Name MATCHES (•Date added)_(•rest of name)
    Subfolder depth IS 0

Then
    Sort into subfolder WITH PATTERN (•date added)
    Rename WITH PATTERN (•date added)_(#) (•rest of name)


Code: Select all
Delete Empty Folder

if (all) of the following conditions are met for (current file or folder)
    Kind IS folder
    Size IS 0 bytes

Then
    Move to folder (Trash)


Code: Select all
Run on Folder Content

if (all) of the following conditions are met for (current file or folder)
    Kind IS folder

Then
    Run rules on folder contents


Code: Select all
Move to Parent if Good Format
 
if (all) of the following conditions are met for (current file or folder)
    Kind IS NOT Folder
    Name MATCHES (123)_(123) (...)
    Subfolder depth IS 1

Then
    Move to folder (THE PARENT FOLDER...)



Basically here's what we're doing:

Take a poorly formatted picture and add a date created prefix, sort that into a subfolder based on that prefix, sequentially name within that subfolder, and bring the renamed file back to the parent folder, deleting the subfolder in the process.

It works for me!
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Wow, beautiful. I wanted to change one thing and managed to break it though. Can you help me understand some of the steps so that I don't break it? The tweak is:
Step (1) sometimes I want to append the name of .mov and .avi movie files that my camera takes and leave them mixed in with the images; also sometimes I have .png screen shots in the same folder that I do NOT want to assign this renaming to, so I changed the Ignore Rule to Extension > Is Not > mov ,avi, jpg
(I don't know how to do the fancy CODE boxes you did to show this)
Now the .mov etc. files get renamed like the images but they get stuck in their subfolder rather than being moved back to the parent folder. Here's what my tweak looks like:
http://dl.dropbox.com/u/2813521/pix%20t ... hazelrules

Can you explain what to fix or change in the other rules to make it so my first change doesn't break the rest of the action?

Also, I don't understand some of the elements in some of the rules. Would you mind explaining?
In Step (4) "Name MATCHES (•Date added)_(•rest of name)" why is this Date added instead of Date created?
In Step (7) "Name MATCHES (123)_(123) (...)" Is the (123) representative of any number? Why not put the custom date format here?

Thanks so much for your generous help!
abbimj
 
Posts: 5
Joined: Fri Mar 30, 2012 12:46 pm

abbimj wrote: Can you explain what to fix or change in the other rules to make it so my first change doesn't break the rest of the action?

Also, I don't understand some of the elements in some of the rules. Would you mind explaining?
In Step (4) "Name MATCHES (•Date added)_(•rest of name)" why is this Date added instead of Date created?
In Step (7) "Name MATCHES (123)_(123) (...)" Is the (123) representative of any number? Why not put the custom date format here?


In step 4, the (•date added) was just a typo on my part! Whoops... But really, it doesn't matter because that field is a custom token which matches a simple numeric pattern.

In step 7, we're not using custom dates for this so its easier and faster to just drag the (123) number match, rather than creating a custom token.

For adding movies to the renaming and excluding PNGs we can do this by holding the OPTION key when hitting (+) to add criteria:

Code: Select all
Ignore Certain Files

if (any) of the following conditions are met for (current file or folder)
    if (all) all of the following:
        Kind IS NOT image
        Kind IS NOT movie
        Kind IS NOT folder
    if (all) of the following:
        Extension is PNG

Then (ignore)


Updated:

http://dl.dropbox.com/u/23950/Hazel%20Rules/Hazel%20Testing.hazelrules
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

a_freyer: Thanks for your help with these issues. And yes, the numbering is per folder so the technique of sorting it then moving it back up is probably the best way to do this short of a script to do the numbering yourself.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support