Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the Last Modified Date of a File in Cocoa

Tags:

cocoa

How can I find the last modified date of a file in cocoa?

like image 439
MobX Avatar asked Jun 30 '09 13:06

MobX


3 Answers

Check out NSFileManager's

- (NSDictionary *)fileAttributesAtPath:(NSString *)path traverseLink:(BOOL)flag

the key you're interested in is NSFileModificationDate.

like image 122
diciu Avatar answered Oct 18 '22 19:10

diciu


Just to update the code:

NSString * path = ... your path here ...
NSDate * fileLastModifiedDate = nil;

NSError * error = nil;
NSDictionary * attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
if (attrs && !error)
{
    fileLastModifiedDate = [attrs fileModificationDate];
}
like image 43
UJey Avatar answered Oct 18 '22 17:10

UJey


Adding this answer here since this was the first result when I searched for how to do this, but if you're using swift you might like this extension:

extension NSFileManager {

    func modificationDateForFileAtPath(path:String) -> NSDate? {
        guard let attributes = try? self.attributesOfItemAtPath(path) else { return nil }
        return attributes[NSFileModificationDate] as? NSDate
    }

    func creationDateForFileAtPath(path:String) -> NSDate? {
        guard let attributes = try? self.attributesOfItemAtPath(path) else { return nil }
        return attributes[NSFileCreationDate] as? NSDate
    }


}
like image 36
BarrettJ Avatar answered Oct 18 '22 17:10

BarrettJ