Intentionally Changing the "Date Modified" attribute

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

First, the tip. You can easily modify the date modified attribute of any file or folder by using a simple shell script. It can be applied to a specific file or folder or used to batch change items in a directory/folder.

Use the embedded shell script in Hazel set to bin/sh. The date format for modifying needs to be in the following format: YYYYMMDDhhmm (ie May 12, 2010 at 12:30pm equals 201005121230)

For a specific item:
Code: Select all
touch -mt YYYYMMDDhhmm [path to file or folder]

For a specific directory for batch change:
Code: Select all
touch -mt YYYYMMDDhhmm [path to file or folder]/*
Notice the appended "/*" to the end of the path

Note, that you can batch process only certain files within a directory. For example, if you had a folder where you wanted to modify the modified date only for PDF files, you would append a /* .PDF

Now the why.

There are many scenarios where you may want to do this. For example, changing attributes like comments and color label do not affect date modified. Perhaps you want to run an action any time you change any attribute of a file or folder. Or another example is in my case, I am slowly converting all my DVDs to M4v. When I rip the DVD, the modified date is the start of the conversion but it does not continue modifying the folder as it processes. Therefore i could have incorporate a script that when the file is done ripping, change modified date to current date. Many people may choose to just use colors but this provides another option. One last example is that when you download files from the internet, they have various modified dates. Perhaps you want to use this in combination with the date added attribute since date added is only captured (and specific to Hazel) and you want the file to carry the date modified attribute around.
dhy8386
 
Posts: 94
Joined: Tue Nov 09, 2010 12:19 pm

Based on macsparky's stringent file naming convention ('YYYY-MM-DD - <whatever>') I amended my workflow to make sure I save the files with the date prefixed that they were originally created. This usually is important for scanned PDFs, which might have a different "written on" date then the scanned date.

What bothered me though was that I want to have a Hazel action to automatically set the date modified and here the touch command with a little bit of sed helped. The action looks like this:

For all files where "name" "matches" "Datestamp" "anything" "Run shell script" "embedded script":
Code: Select all
x=`echo $1 | sed 's/\(....\)-\(..\)-\(..\) .*/\1\2\3/'`
touch -t "$x"0000 "$1"

where "Datestamp" is a custom token "123-123-123 -"

Downside of it is... it will always match so we have a continous touching of the file, but that doesn't bother me much as the action folder is emptied usually VERY quickly.
sussdorff
 
Posts: 2
Joined: Fri Jul 27, 2012 4:05 am

I am trying to do something similar with date created using the setfile command. My embedded shell script is as follows:

Code: Select all
x=`echo $1 | sed 's/\(....\)-\(..\)-\(..\) \(..\)\.\(..\)\.\(..\) .*/\2\/\3\/\1\ \4\:\5\:\6/'`
setfile -d "$x" "$1"


The filename follows the format: 2009-12-12 10.11.12 Robin_001.png

When I execute the sed command from the terminal, I get a value of "12/12/2009 10:11:12" which is what setfile wants. I am guessing my problem is in the quotes since there is a space inside of $X. Any thoughts?
learned
 
Posts: 21
Joined: Sun Jun 21, 2009 5:18 pm

What's the actual problem?
Mr_Noodle
Site Admin
 
Posts: 8571
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Tips & Tricks - DO NOT POST QUESTIONS

cron