views:

178

answers:

1

hi all. I'm trying to find the creation date (NOT modification date) of a file.

Creation date doesn't appear to be in the attributes of a file, though modified date is.

I'm using this code..

NSFileManager* fm = [NSFileManager defaultManager];

NSString* path = [PathHelpers pathInDocumentsFolderWithFilename:FILE_NAME];
NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil];

if (attrs != nil) {
    return (NSDate*)[attrs objectForKey: NSFileCreationDate];
} else {
    return nil;
}

This always returns nil. Typing 'po attrs' into the debugger to get the list of key/value pairs in the NSDictionary returns the following..

{
NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = 2010-01-21 11:47:55 +0000;
NSFileOwnerAccountID = 501;
NSFileOwnerAccountName = ben;
NSFilePosixPermissions = 420;
NSFileReferenceCount = 1;
NSFileSize = 338;
NSFileSystemFileNumber = 2234;
NSFileSystemNumber = 42553324;
NSFileType = NSFileTypeRegular;

}

No creation date.. bah..

Anyone know another way of getting the creation date or does it just not exist in iPhone?

Thanks a lot, Ben

+1  A: 

Can you use fstat64 to get the st_birthtimespec member of the returned struct? You'll need to create a C file handle for the file and convert the timespec value into an NSDate, but that's better than nothing.

Jeff Kelley
No, you should not be able to use fstat64 owing to the fact that, to the best of my knowledge iPhone OS is not yet 64 bits. Unfortunately, in 32 bits mode, the struct timespec st_birthtimespec is NOT available in struct stat. This is the reason why the NSDate object returned by [attrs fileCreationDate] will be always nil on the iPhone as of 3.1.2, where attrs is the NSDictionary containing the file attributes.
unforgiven
Thanks guys. Looks like I'll just have to remember when the file was created and store it in NSUserDefaults.
Ben Clayton