Skip to content

Instantly share code, notes, and snippets.

@atr000
Created February 8, 2010 05:16
Show Gist options
  • Save atr000/297894 to your computer and use it in GitHub Desktop.
Save atr000/297894 to your computer and use it in GitHub Desktop.
//
// created 07.30.2009 by Hank McShane
// version: 0.1
// 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("Usage:\nChangeFileDates -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* dict = [NSDictionary dictionaryWithObjectsAndKeys:createDate, NSFileCreationDate, modDate, NSFileModificationDate, nil ];
NSError* error;
[[NSFileManager defaultManager] setAttributes:dict ofItemAtPath:file error:&error];
if (error) {
printf("\nThere was an error:\n%s\n", [[error localizedDescription] UTF8String]);
return 1;
}
[pool drain];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment