Question

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 myFilePath 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 contentsOfDirectoryAtPath 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?

Was it helpful?

Solution 2

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 ;)

OTHER TIPS

Swift 3 solution:

func fileModificationDate(url: URL) -> Date? {
    do {
        let attr = try FileManager.default.attributesOfItem(atPath: url.path)
        return attr[FileAttributeKey.modificationDate] as? Date
    } catch {
        return nil
    }
}

Here is a Swift like solution of @zvjerka24 answer:

func lastModified(path: String) -> NSDate? {
    let fileUrl = NSURL(fileURLWithPath: path)
    var modified: AnyObject?
    do {
        try fileUrl.getResourceValue(&modified, forKey: NSURLContentModificationDateKey)
        return modified as? NSDate
    } catch let error as NSError {
        print("\(#function) Error: \(error)")
        return nil
    }
}

If you get the error:

"CFURLCopyResourcePropertyForKey failed because it was passed this URL which has no scheme"

You can try to solve this by appending "file:///" to your NSString file path before converting it to NSURL, it worked in my case.

Can also do:

NSURL* file = ...
NSError* error;`
NSDate *creationDate = [[NSFileManager defaultManager] attributesOfItemAtPath:file.path error:&error].fileCreationDate;

For any file in macOS system we can easily get modification date by using any of below mentioned options:

Way 1:

  • NSString *path = @"path to file";
  • NSError *err = nil;
  • NSDictionary *dic2 = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&err];
  • NSLog(@"File modification Date:%@", dic2[NSFileModificationDate]);

Way 2:

  • MDItemRef itemRef = MDItemCreate(kCFAllocatorDefault, (__bridge CFStringRef)path);
  • NSArray *attributeNames = (__bridge NSArray *)MDItemCopyAttributeNames(itemRef);
  • NSDictionary *attributes = (__bridge NSDictionary *) MDItemCopyAttributes(itemRef, (__bridge CFArrayRef) attributeNames);

  • CFDateRef modifDate = MDItemCopyAttribute(itemRef, kMDItemContentModificationDate);

  • NSDate* modificationDate = (__bridge NSDate*) modifDate;
  • NSLog(@"Modification Date%@", modificationDate);

You can also print various other attributes provided by MDItem : NSLog(@"All attributes%@", attributes);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top