I'm trying to set the filesystem creation time for a file on Mac OS using a ruby script.
On Mac OS X the 'ctime' represents the last time of inode modification rather than the file creation time, thus using ruby's File.utime() to set ctime will not help.
Using this hint [ http://inessential.com/2008/12/18/file_creation_date_in_ruby_on_macs ] I can retrieve a file's creation time:
Time.parse(`mdls -name kMDItemContentCreationDate -raw "#{filename}"`)
...but any idea on how to set it using ruby?
-- UPDATE --
Okay, I think I can actually do this with File.utime
in ruby.
Even though the ctime is technically not used by Mac OS to track file creation time, when you use utime
to update the ctime (along with the mtime, which must be simultaneously set) the filesystem appears to magically also update the creation time as per kMDItemContentCreationDate
.
So to set filename to a ctime of 1st Oct 2010 and a mtime of 2nd Oct 2010:
File.utime(Time.strptime('011010', '%d%m%y'), Time.strptime('021010', '%d%m%y'), filename)
There is a Ruby solution with the method utime
. But you will have to set modification time (mtime) and access time (atime) at once. If you want to keep access time you could use:
File.utime(File.atime(path), modification_time, path)
See Ruby core documentation as well.
So you've definitely got a pure Ruby solution working, but since this is OS X, are you opposed to exec()
or system()
and just using touch
? In your case, I'd almost prefer:
system "touch -t YYYYMMDDhhmm /what/ever"
if for no other reason than clarity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With