How can I find the last modified date of a file in cocoa?
Check out NSFileManager's
- (NSDictionary *)fileAttributesAtPath:(NSString *)path traverseLink:(BOOL)flag
the key you're interested in is NSFileModificationDate.
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];
}
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With