Robert Robert - 5 months ago 28
Objective-C Question

Retrieve file creation or modification date

I'm using this piece of code to try to retrieve the last modified date of a file:

NSError *error = nil;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath: myFilePath error:&error];

if (attributes != nil) {
NSDate *date = (NSDate*)[attributes objectForKey: NSFileModificationDate];
NSLog(@"Date modiifed: %@", [date description]);
else {
NSLog(@"Not found");

This works well for files in the main bundle but not if the file is located in a subdirectory of the app's document folder, with
like this:

/Users/User/Library/Application Support/iPhone Simulator/6.0/Applications/The App ID Number/Documents/mySubdirectory/My Saved File

It keeps returning "not found".

I know the file is there, as I can view it with finder. I also tried removing the spaces in the file name but this had no effect.

The error log says no such file or directory, so it looks like something must've gone wrong when I tried to copy the file to the document directory.

Weird thing is, iterating through the document sub directory with
shows the file as being present.

I've tried hard-coding the path and retrieving it programmatically, with:

*myFolder = [documentsDirectory stringByAppendingPathComponent:@"myFolder"];
*myFilePath = [myFolder stringByAppendingPathComponent:theFileName];

Can anyone see where I'm going wrong?


Try this. I had same problem and solved with something like next:

NSURL *fileUrl = [NSURL fileURLWithPath:myFilePath];
NSDate *fileDate;
[fileUrl getResourceValue:&fileDate forKey:NSURLContentModificationDateKey error:&error];
if (!error)
//here you should be able to read valid date from fileDate variable

hope it helped ;)