Tuesday, September 2, 2014

#1 2009-07-28 01:38:39 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

setting creation and modification date problem

I'm trying to set the creation and modification dates of a file. I'm using the "SetFile" unix executable to do this. Note that this exe file is only installed if you have the developer tools installed. The problem is that the file time that gets set is one hour less than the time I have chosen. Try this script and see for yourself. Any ideas how to solve this? I assume it has something to do with daylight savings time but I'm not sure how to fix it. I'm in the US Eastern Time Zone.

Note: this script creates a new file on the desktop using "touch" and then sets its dates. The actual creation and modification dates of the file are returned at the end of the script. You'll see that the times are different from what I chose.

Applescript:

-- this will create the follwoing file on your desktop and set its creation and modification dates
set aFile to (path to desktop folder as text) & "test.txt" -- a dummy file to test with
set {creationDate, modificationDate} to {"10/03/2008 14:28:10", "07/23/2009 02:07:47"} -- some arbitrary dates

-- create the file, delete it if it exists
try
   do shell script "rm " & quoted form of POSIX path of aFile
end try
do shell script "touch " & quoted form of POSIX path of aFile

-- set creation and modification dates of a file
do shell script "/Developer/Tools/SetFile -d " & quoted form of creationDate & space & quoted form of POSIX path of aFile
do shell script "/Developer/Tools/SetFile -m " & quoted form of modificationDate & space & quoted form of POSIX path of aFile

-- check the new dates of the file
set cd to do shell script "/Developer/Tools/GetFileInfo " & quoted form of POSIX path of aFile & " | grep created | cut -c 10-"
set md to do shell script "/Developer/Tools/GetFileInfo " & quoted form of POSIX path of aFile & " | grep modified | cut -c 11-"
return {cd, md}

Offline

 

#2 2009-07-28 01:47:13 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: setting creation and modification date problem

Hey Hank,

When I run the above script this is what is returned.

{
    "10/03/2008 14:28:10",
    "07/23/2009 02:07:47"
}

Offline

 

#3 2009-07-28 01:50:51 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: setting creation and modification date problem

Hi Craig,

I get this returned...
{"10/03/2008 13:28:10", "07/23/2009 01:07:47"}

I notice that by changing the month value that if it's anything from 04 to 11 it is one hour less, the other months are created properly. That's why I think it's a daylight savings time thing. You must have some different DST factor.

Offline

 

#4 2009-07-28 01:54:45 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: setting creation and modification date problem

You are probably right. Any ideas how you will fix it or account for it?

Offline

 

#5 2009-07-28 01:55:54 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: setting creation and modification date problem

Craig Williams wrote:

Hey Hank,

When I run the above script this is what is returned.

{
    "10/03/2008 14:28:10",
    "07/23/2009 02:07:47"
}

Weird... I changed my time zone in the Date & Time preference pane to Denver. I assume that's what you are using. I still get the improper time with that setting... so I'm confused.

Offline

 

#6 2009-07-28 02:06:15 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: setting creation and modification date problem

I think what is really weird is that no matter what your date/time settings are it should not effect a time you are specifically setting on a file. What you set should be what you get back. Something else seems to be going on here. I am searching the net to see if anyone has posted similar situation.

Offline

 

#7 2009-07-28 02:10:14 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: setting creation and modification date problem

Out of curosity, what is the mod date if you open the file, make a change and close it?

Is it still an hour earlier?

Offline

 

#8 2009-07-28 02:11:56 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: setting creation and modification date problem

Craig Williams wrote:

Out of curosity, what is the mod date if you open the file, make a change and close it?

Is it still an hour earlier?

No, the mod date is right... 3:09 PM here on the east coast.

Offline

 

#9 2009-07-28 02:29:08 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: setting creation and modification date problem

Try this script and see what the results are.

#!/usr/bin/env ruby -w

# This will set the mod time to 2 hours ago

# Change path
file = "/users/USER_NAME/desktop/test.txt"
File.utime(0, Time.now - 7200, file)

Offline

 

#10 2009-07-28 02:38:32 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: setting creation and modification date problem

Yes, that script works. The problem is it doesn't do anything to the creation date. I want that ability. I use this when I'm backing up files with Chronosync. If the creation or mod dates are different a backed-up file is overwritten, so I use this when I don't want a particular file to be overwritten when backing up.

Offline

 

#11 2009-07-28 02:57:22 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: setting creation and modification date problem

True, but at least this narrows down the issue to the SetFile command and not something with your system or time settings.

Have you considered writing a foundation tool?

Offline

 

#12 2009-07-28 03:12:39 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: setting creation and modification date problem

Well if nobody has any ideas, then I'll have to try something more drastic... but I still have hope somebody has an idea how to work around this.

Offline

 

#13 2009-07-28 03:29:11 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: setting creation and modification date problem

If you us -mt it changes both created and modified dates. You could then issue another command to change the modified to a different date.

touch -mt 200701120000 /path/to/test.txt

So, as an example:

file = "/users/craig/desktop/test.txt"
`touch -mt 200201120000 #{file}`
File.utime(0, Time.now - 7200, file)

Offline

 

#14 2009-07-29 10:41:55 am

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: setting creation and modification date problem

Craig Williams wrote:

If you us -mt it changes both created and modified dates. You could then issue another command to change the modified to a different date.

touch -mt 200701120000 /path/to/test.txt

So, as an example:

file = "/users/craig/desktop/test.txt"
`touch -mt 200201120000 #{file}`
File.utime(0, Time.now - 7200, file)

Unfortunately this doesn't work right. It only impacts the creation date of a file if the mod date you are setting is before the current creation date. For example, using the text.txt file we created with a creation date of 10/03/2008. If you touch it with a date of 11/03/2008 as you suggest then the creation date stays as 10/03/2008. So touch doesn't really change creation dates. Thanks for the suggestion though.

Offline

 

#15 2009-07-29 02:16:10 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: setting creation and modification date problem

I am guessing you have already done this but if not... smile

There could be better error checking and you could have the tool process all the files instead of passing them in one at at time but it appears to be working.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSArray *args = [[NSProcessInfo processInfo] arguments];
   
    if ( argc != 4 ) {
        printf(
            "\nUsage: \n\tChangeFileDates filePath creationDate modDate\n"
            "\tYou can use dates like '12/01/10' or 'Last Tuesday at 12:00'\n\n"
        );
        return 1;
    }
   
    // Arguments
    NSString *path    = [[args objectAtIndex:1] retain];
    NSString *cDate    = [[args objectAtIndex:2] retain];
    NSString *mDate    = [[args objectAtIndex:3] retain];
   
    NSError *error;
    NSFileManager *NSFm = [NSFileManager defaultManager];
   
    NSDate *createDate    = [NSDate dateWithNaturalLanguageString:cDate];
    NSDate *modDate        = [NSDate dateWithNaturalLanguageString:mDate];
   
    NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
                       createDate, NSFileCreationDate,
                       modDate, NSFileModificationDate,
                       nil ];
   
    [NSFm setAttributes:d ofItemAtPath:path error:&error];
   
    if ( error != NULL ) printf("\nThere was an error");
   
    [pool drain];
    return 0;
}

Offline

 

#16 2009-07-29 08:54:02 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: setting creation and modification date problem

Craig Williams wrote:

I am guessing you have already done this but if not...

Excellent Craig. I didn't realize there were NSFileCreationDate and NSFileModificationDate cocoa attributes for NSFileManager... so no I haven't tried that. But i definitely will! Thanks. I'll report back how it works when I get the chance to try your code... probably tomorrow.

Offline

 

#17 2009-07-30 10:40:58 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: setting creation and modification date problem

Hi

regulus6633 wrote:

{"10/03/2008 13:28:10", "07/23/2009 01:07:47"}

I notice that by changing the month value that if it's anything from 04 to 11 it is one hour less, the other months are created properly. That's why I think it's a daylight savings time thing. You must have some different DST factor.

It's the same thing for me here.

My solution is to use the "SetFile" (May 2007 or older) instead of the "SetFile" (September 2007 or newer).

You can find it in the "Xcode Tools" on Tiger.

Or in the ( MacOSXUpdate, MacOSXUpdCombo ) package :
SetFile is in the the resources folder in a package like this (/Library/Receipts/MacOSXUpd10.4.10.pkg)


One mans trash is another mans treasure

Offline

 

#18 2009-07-30 10:50:04 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10490
Website

Re: setting creation and modification date problem

Craig Williams wrote:

// Arguments
    NSString *path    = [[args objectAtIndex:1] retain];
    NSString *cDate    = [[args objectAtIndex:2] retain];
    NSString *mDate    = [[args objectAtIndex:3] retain];

Hi Craig,

memory management alert! If you retain objects, you are responsible to release them.
This is not so important in a CLI, which will be terminated at once, but you should always consider memory management.
In this case you can omit all retain messages, they are not necessary


regards

Stefan

Offline

 

#19 2009-07-30 11:11:10 am

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: setting creation and modification date problem

So true. I have been building FileMaker Pro databases and websites for the last few months and this is the first Objective-C code I have written in a while. How fast we forget the basics! wink

Most of the time I would rather retain and then release though, as Hank can testify, autorelease can bite you in the butt. If you retain it, you don't have to worry whether it is going to be there when you want to use it.

Offline

 

#20 2009-07-30 11:22:33 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10490
Website

Re: setting creation and modification date problem

Craig Williams wrote:

If you retain it, you don't have to worry whether it is going to be there when you want to use it.

In the same scope you don't have to worry. The system will keep args alive during the whole method

Last edited by StefanK (2009-07-30 11:22:46 am)


regards

Stefan

Offline

 

#21 2009-07-30 11:54:46 am

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: setting creation and modification date problem

Jacques wrote:

It's the same thing for me here.
My solution is to use the "SetFile" (May 2007 or older) instead of the "SetFile" (September 2007 or newer).

I have an old 10.4 install with SetFile from Sept 12, 2006. Using that gives me the same results so this didn't solve the problem for me. I'm going to try Craig's obj-c code now and will report back.

Offline

 

#22 2009-07-30 02:03:57 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: setting creation and modification date problem

Thank you Craig! big_smile

Using your suggested code I finally have a working solution! I posted the tool, the code, and a suggested applescript for using it on my website. You can see it here: http://www.hamsoftengineering.com/codeS … Dates.html

Offline

 

#23 2009-07-30 02:14:41 pm

Craig Williams
Administrator
From: Ft. Smith, AR
Registered: 2006-12-06
Posts: 887

Re: setting creation and modification date problem

You are very welcome. Glad I could help!

I like all the error checking. That makes this very useful and distributable.

Good job on the website post. I am sure there are a lot of people who will find this useful. There really is very little on the internet on how to do this.

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.042 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)