Reputation: 6337
I was planning on writing some code whose logic was based upon testing the creation date of a particular file in my app's Documents folder. Turns out, when I call -[NSFileManager attributesOfItemAtPath:error:], NSFileCreationDate isn't one of the provided attributes.
Is there no way to discover a file's creation date?
Thanks.
Upvotes: 7
Views: 5051
Reputation: 40502
The fileCreationDate is indeed part of the dictionary. Here's a method that gets passed a file URI and grabs some of the attributes from the file:
- (NSDictionary *) attributesForFile:(NSURL *)anURI {
// note: singleton is not thread-safe
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *aPath = [anURI path];
if (![fileManager fileExistsAtPath:aPath]) return nil;
NSError *attributesRetrievalError = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:aPath
error:&attributesRetrievalError];
if (!attributes) {
NSLog(@"Error for file at %@: %@", aPath, attributesRetrievalError);
return nil;
}
NSMutableDictionary *returnedDictionary =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
[attributes fileType], @"fileType",
[attributes fileModificationDate], @"fileModificationDate",
[attributes fileCreationDate], @"fileCreationDate",
[NSNumber numberWithUnsignedLongLong:[attributes fileSize]], @"fileSize",
nil];
return returnedDictionary;
}
Upvotes: 8
Reputation: 27601
According to Apple's reference, NSFileCreationDate
is available in 2.0+:
NSFileCreationDate The key in a file attribute dictionary whose value indicates the file's creation date.
The corresponding value is an NSDate object.
Available in iPhone OS 2.0 and later.
Declared in NSFileManager.h.
Upvotes: 1