views:

176

answers:

3

Is it possible to get file/folder last accessed date 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);

As per the above code i have tried both UTF8String and fileSystemRepresentation, but both are giving me current date and time.Please let me know if i am doing anything wrong.

+1  A: 

Visit http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/attributesOfItemAtPath:error:

and read about: attributesOfItemAtPath:error:

Regards Friedrich

Friedrich
Thanks for the link, but my question is regarding getting File Last Accessed Date just like mirosoft windows.Right now i can only get the following dates NSFileModificationDate and NSFileCreationDate of file.So i am expecting anything like NSFileLastAccessedDate..If it is possible to get in mac.
AmitSri
+2  A: 

The C way of doing it, using the stat system call will work in Objective-C.

e.g.

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 you might get what you want is to construct an NSURL fore a file URL pointing to the file you want and using -resourceValuesForKeys:error: to get the NSURLContentAccessDate resource value.

JeremyP
The one catch with Jeremy's Cocoa method is that it's 10.6-only. There's no file access analogue in the pre-10.6 APIs, so you have to fall back to the stat method.
Jablair
@jablair: good point. I just checked the docs and it's also not available on iPhone.
JeremyP
Thanks, for the code, I'll try and let you know.Also thanks for mentioning the Api version. Right now i am developing using 10.5, so will go with the stat method.
AmitSri
Sorry, i am new to mac development, so don't have any idea of using your code in my cocoa application. It will be great help, if you explain me how i can incorporate your stat method with objective-c code.
AmitSri
Amit: `aFilePath` needs to be a UTF-8-encoded C string. If you have the pathname as an NSString object, you can ask it for a `UTF8String`, which is what you need.
Peter Hosey
Thanks again,I'll try and let you know
AmitSri
K,i tried and getting following error for first linestruct stat output;error: storage size of 'output' isn't knownAm i missing something here?
AmitSri
Sorry, I fixed the error by adding #include <sys/stat.h>
AmitSri
getting error on struct timespec accessTime = output.st_atime;error: invalid initializerit seems that output.st_atime is not correct,i changed it with followingstruct timespec accessTime = output.st_atimespec; and it seems to be work
AmitSri
Thanks,all seems to be working except one last thing.How to convert struct timespec accessTime to NSDate?
AmitSri
I tried following but it is giving me current date and time NSDate *aDate = [NSDate dateWithTimeIntervalSince1970:accessTime.tv_sec];is that correct?
AmitSri
Amit: That line, in isolation, appears to be correct. If you're still having problems with this solution, you should edit a fuller code snippet into your question.
Peter Hosey
@Peter Hosey: Do not use UTF8String to get the C string, use filseSystemRepresentation as I stated in my answer.
JeremyP
Hi,thanks again i problem is remains same, so i have edited my question with full code, please let me know if i am doing anything wrong.
AmitSri
@amit: So the file you are trying to get the access time for is one that was selected using an open dialog? I would not be at all surprised if the open panel accesses the file itself to get some of the data it needs. Hence access time will always be "just now" with your code.
JeremyP
Yes, the file which i am passing to the function is coming from open panel.I don't think i am accessing the file or its data before passing it to the above function. Another thing is when i rechecked the file Last open Date in finder, it is showing the correct DateTime which is not the current one.So, it might be possible that i am doing something wrong or there is something wrong with open panel which is automatically open the file somehow.
AmitSri
Clearly then the last open date in finder is not the atime attribute that you get back from stat. I'm fairly sure that atime will be changed when the open panel touches the file so that it can display various information in the panel. Unless you can target 10.6 or later, I'm not sure what else you can do.
JeremyP
Thanks,i'll try that later now or may be in next version of my application in 10.6.Right now, i have to stick with the 10.5 to completed current version.
AmitSri
Anyways, i have also tried to give hard wire file path and the result is same.so there might be something else which is missing in my side.I wish i could get some working sample to verify the same here.
AmitSri
+1  A: 

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

regulus6633
The catch is that this will only work if Spotlight is turned on, has indexed the volume the file is on, and is not currently re-indexing the volume the file is on.
Peter Hosey
Thanks,you are correct,we can't trust on spotlight feature, as if it turned off, I'll loose the functionality.
AmitSri
And of course, if Spotlight *is* turned on, it immediately renders the last access time meaningless. The last access time could easily be the last time spotlight indexed the file.
JeremyP