Find Last Available File Date in Cocoa

Is it possible to get the last access date of a file / folder in mac using cocoa?

struct stat output; //int ret = stat([[[openPanel filenames] lastObject] UTF8String], &output); int ret = stat([[[openPanel filenames] lastObject] fileSystemRepresentation], &output); // error handling omitted for this example struct timespec accessTime = output.st_atimespec; NSDate *aDate = [NSDate dateWithTimeIntervalSince1970:accessTime.tv_sec]; NSLog(@"Access Time %d, %@",ret, aDate); 

According to the code above, I tried both UTF8String and fileSystemRepresentation, but both give me the current date and time. Please let me know if I am doing something wrong.

+6
objective-c cocoa
source share
4 answers

Method C, using the stat system call, will work in Objective-C.

eg.

 struct stat output; int ret = stat(aFilePath, &output); // error handling omitted for this example struct timespec accessTime = output.st_atime; 

You should get aFilePath by sending -fileSystemRepresentation to an NSString containing the path.

Another way that you can get what you need is to create an NSURL for the file pointing to the desired file, and using - resourceValuesForKeys: error: to get the value of the NSURLContentAccessDate resource.

+10
source share

Using NSMetadataQuery, you can access the searchlight metadata from your code. The last used file date attribute is tracked by a spotlight, and you can access this property: kMDItemLastUsedDate.

+1
source share
 #include <sys/stat.h> -(NSDate *)getFileAccessLastDateOfFile:(NSString *)aFilePath{ struct stat output; int ret = stat([aFilePath fileSystemRepresentation], &output); struct timespec accessTime = output.st_atimespec; NSDate *aDate = [NSDate dateWithTimeIntervalSince1970:accessTime.tv_sec]; return aDate; } 
0
source share

All Articles