ChangeFileDates
version 0.3
Requires Mac OS X 10.5 or higher


HAMSoft Engineering allows free use of this code and/or software in its "as is" condition. HAMSoft Engineering disclaims any liability of any kind for any damages whatsoever resulting from the use of this code and/or software. If you find it useful please consider making a donation to help HAMSoft Engineering stay in business.

download the compiled command line tool

Use this command line tool to set the creation and modification dates of a file. Thanks to Craig Williams for his code suggestion which can be seen here: http://macscripter.net/viewtopic.php?id=29902

Usage:
ChangeFileDates -cDate creationDate -mDate modificationDate -file filePath

The input variables explained:
[-cDate "date"] the creation date
[-mDate "date] the modification date
[-file "path/to/file"] the file to change

NOTE: Dates should be in the format 'mm/dd/yyyy hh:mm:ss'.


Source Code for ChangeFileDates.m:

//

// created 07.30.2009 by Hank McShane

// last modified 01.31.2011 by Hank McShane

// version: 0.3

// requires: Mac OS X 10.5 or higher

// Thanks to Craig Williams for his code suggestion which can be seen here:

// http://macscripter.net/viewtopic.php?id=29902


// Usage:

// ChangeFileDates -cDate creationDate -mDate modificationDate -file filePath


// This foundation tool sets the creation and modification dates of the file.

// Dates should be in the format 'mm/dd/yyyy hh:mm:ss'.

//


#import <Foundation/Foundation.h>


int main (int argc, const char * argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// see if help is being requested

NSArray* pInfo = [[NSArray alloc] initWithArray:[[NSProcessInfo processInfo] arguments]];

if ([pInfo count] == 1 || [[pInfo objectAtIndex:1] isEqualToString:@"-h"] || [[pInfo objectAtIndex:1] isEqualToString:@"-help"]) {

printf("ChangeFileDates version 0.2\nUsage: ChangeFileDates -cDate creationDate -mDate modificationDate -file filePath\nThis foundation tool sets the creation and modification dates of the file.\nDates should be in the format 'mm/dd/yyyy hh:mm:ss'.\n\n");

return 1;

}

[pInfo release];

// check the creation date

NSString* cDate = [[NSUserDefaults standardUserDefaults] stringForKey:@"cDate"];

if (cDate == NULL) {

printf("ERROR: Missing the cDate argument\n");

return 1;

}

NSDate* createDate = [NSDate dateWithNaturalLanguageString:cDate];

if (!createDate) {

printf("ERROR: cDate is in the wrong format.\nUse a date format of 'mm/dd/yyyy hh:mm:ss'\n");

return 1;

}

// check the modification date

NSString* mDate = [[NSUserDefaults standardUserDefaults] stringForKey:@"mDate"];

if (mDate == NULL) {

printf("ERROR: Missing the mDate argument\n");

return 1;

}

NSDate* modDate = [NSDate dateWithNaturalLanguageString:mDate];

    if (!modDate) {

printf("ERROR: mDate is in the wrong format.\nUse a date format of 'mm/dd/yyyy hh:mm:ss'\n");

return 1;

}

// make sure the modification date is not earlier than the creation date

NSDate* earlierDate = [createDate earlierDate:modDate];

if (!(earlierDate == createDate)) {

printf("ERROR: the modification date cannot be earlier than the creation date.\n");

return 1;

}

// check the file path

NSString* file = [[NSUserDefaults standardUserDefaults] stringForKey:@"file"];

if (file == NULL) {

printf("ERROR: Missing the file argument\n");

return 1;

}

if (![[NSFileManager defaultManager] fileExistsAtPath:file]) {

printf("ERROR: the file could not be found.\n%s\n", [file UTF8String]);

return 1;

}

// everything is ok so set the dates!

NSDictionary* modDict = [NSDictionary dictionaryWithObject:modDate forKey:NSFileModificationDate];

NSDictionary* createDict = [NSDictionary dictionaryWithObject:createDate forKey:NSFileCreationDate];

NSError* error = nil;

// I need to set the dates separately instead of at once because it doesn't work properly if you are changing the create date to a newer date than the file already has.

// set the modification date

    [[NSFileManager defaultManager] setAttributes:modDict ofItemAtPath:file error:&error];

    if (error) {

printf("\nThere was an error:\n%s\n", [[error localizedDescription] UTF8String]);

return 1;

}

// set the creation date

[[NSFileManager defaultManager] setAttributes:createDict ofItemAtPath:file error:&error];

    if (error) {

printf("\nThere was an error:\n%s\n", [[error localizedDescription] UTF8String]);

return 1;

}

    [pool drain];

    return 0;

}



Example usage of the tool with applescript:
Open this Script in your Editor

-- this will change the creation and modification dates of a chosen file

set changeFileDatesPath to (path to home folder as text) & "UnixBins:ChangeFileDates" -- path to unix exe

set creationDate to "10/03/2008 04:28:10"

set modificationDate to "07/30/2009 14:49:00"



set aFile to choose file without invisibles

try

do shell script quoted form of POSIX path of changeFileDatesPath & " -cDate " & quoted form of creationDate & " -mDate " & quoted form of modificationDate & " -file " & quoted form of POSIX path of aFile

on error theError number errorNumber

display dialog "There was an error:" & return & theError & return & return & "Error Number: " & errorNumber as text buttons {"OK"} default button 1 with icon stop

end try