Renaming files with Date Modified as Prefix

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

Moderator: Mr_Noodle

Renaming files with Date Modified as Prefix Fri Dec 02, 2016 11:19 am • by vmax77
Hi

Objective - Rename files that are not folders with the "Date Modified" as the prefix to the file.

Current rule setup

Rule 1 - If Kind is Folder then Run rules on Folder content

Rule 2
If Kind is not Folder then Rename with pattern <date> <filename>.<extension>

Problem
FIrst time the renaming happens its fine, but the after second modification of the file, the first modified date is part of the file name. So file name ends up as <Date 2><Date1> <filename>.<extension>.

What I want to achieve

Check if the filename's first 10 chars(YYYY-MM-DD) is Date Modified.
If Date Modified matches then Do Nothing

If Date Modified does not match, check the first 10 chars form a date (not sure what is the elegant way to do this)
If first 10 chars are date, truncate the current date and rename with Date modified prefix.
If first 10 chars are NOT date, just prefix the with Date modified prefix.
vmax77
 
Posts: 7
Joined: Sun Oct 26, 2014 2:30 pm

What version of Hazel are you running? I did add some extra loop detection in version 4 but if you are running an older version then this might still be an issue.

Regardless, to do what you want, you want to use match patterns. Search for it in the help. You can match on a date using that.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:What version of Hazel are you running? I did add some extra loop detection in version 4 but if you are running an older version then this might still be an issue.

Regardless, to do what you want, you want to use match patterns. Search for it in the help. You can match on a date using that.


HI

I am using V4.0.6
vmax77
 
Posts: 7
Joined: Sun Oct 26, 2014 2:30 pm

vmax77 wrote:
Mr_Noodle wrote:What version of Hazel are you running? I did add some extra loop detection in version 4 but if you are running an older version then this might still be an issue.

Regardless, to do what you want, you want to use match patterns. Search for it in the help. You can match on a date using that.


HI

I am using V4.0.6


First I couldn't get it to filter for files with names starting with date. Also I couldn't use the match patterns to detect for existing date in the front of the filename. Even if that did happen, I can't seem to find any way to strip the date in front of the name to be replaced with new date.
vmax77
 
Posts: 7
Joined: Sun Oct 26, 2014 2:30 pm

Can you post what you have so far?
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:Can you post what you have so far?


This is what I have

http://imgur.com/a/UI7i2
vmax77
 
Posts: 7
Joined: Sun Oct 26, 2014 2:30 pm

Note that when you use "Name matches/does not match", the pattern has to account for everything in the name. In your case, you are only matching names that only consist of a date. If there is any more text after the date, it won't match. If you don't care what occurs after the date, drag in the "anything" token to match anything (or nothing).
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Renaming files with Date Modified as Prefix Sun Dec 11, 2016 11:11 pm • by vmax77
Mr_Noodle wrote:Note that when you use "Name matches/does not match", the pattern has to account for everything in the name. In your case, you are only matching names that only consist of a date. If there is any more text after the date, it won't match. If you don't care what occurs after the date, drag in the "anything" token to match anything (or nothing).


Aha! That helps, thank you very much.

I still seem to struggling to replace the matched date. Or to trim the first characters (date part) of the filename, so the name is only the filename excluding the date modified
vmax77
 
Posts: 7
Joined: Sun Oct 26, 2014 2:30 pm

vmax77 wrote:
Mr_Noodle wrote:Note that when you use "Name matches/does not match", the pattern has to account for everything in the name. In your case, you are only matching names that only consist of a date. If there is any more text after the date, it won't match. If you don't care what occurs after the date, drag in the "anything" token to match anything (or nothing).


Aha! That helps, thank you very much.

I still seem to struggling to replace the matched date. Or to trim the first characters (date part) of the filename, so the name is only the filename excluding the date modified



Update:

Managed to cobble an Applescript together to trim the existing date prefix to the filename.

Code: Select all
set file_name to name of theFile
set datemod to modification date of theFile

if file_name does not start with (current date) then
   set trim_name to text 11 thru -1 of file_name
   set file_name to datemod & " " & trim_name
   set new_name to file_name
end if


This is what I have now
Image

http://i63.tinypic.com/1y61c0.jpg
vmax77
 
Posts: 7
Joined: Sun Oct 26, 2014 2:30 pm

You shouldn't need a script for that. In your pattern, use a custom attribute to match everything after the date. Then use that custom attribute in the rename pattern.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support