Reputation: 865
How can I get the file creation or last modification date of the file at a particular location, e.g. /Users/MYUSER/Downloads/text.txt
?
Upvotes: 15
Views: 11783
Reputation: 38662
NSFileManager *fm = [NSFileManager defaultManager];
// Pre-OS X 10.5
NSLog(@"%@", [[fm fileAttributesAtPath:@"input.txt" traverseLink:YES] fileModificationDate]);
[fm changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate]
atPath:@"input.txt"];
// OS X 10.5+
NSLog(@"%@", [[fm attributesOfItemAtPath:@"input.txt" error:NULL] fileModificationDate]);
[fm setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate]
ofItemAtPath:@"input.txt" error:NULL];
Source: Rosetta Code
Upvotes: 5
Reputation: 865
Example from Rosetta Code has deprecated parts. Here is the right code for getting file modification date
NSString *path = @"/Users/Raven/Downloads/1.png";
NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
NSDate *result = [fileAttribs objectForKey:NSFileCreationDate]; //or NSFileModificationDate
NSLog(@"%@",result);
Upvotes: 45