Objective-C: Get file creation / last modification date?

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


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

Tags:

Objective C