Robert Kang
Robert Kang

Reputation: 568

Is there a way to get folder's creation date in Cocoa?

I tried with NSFileManager's attributesOfItemAtPath. It works well with files, but not with folders. Although in Apple's documentation, they claimed it should work either on file or folder. But all I get is a nil value if I call this on folders.

Code I use:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDictionary *folderAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:somePath error:nil];

NSLog(@"Creation date: %@", [dateFormatter stringFromDate:[folderAttributes objectForKey:NSFileCreationDate]]);

The output is always null to me. My "somePath" is an NSString, with a format like this:

file://localhost/Users/username/...

Thoughts? Thanks!

Upvotes: 3

Views: 470

Answers (2)

Monolo
Monolo

Reputation: 18253

somePath should be formatted as a straight POSIX path - set it to /Users/username/... - that should work.

Upvotes: 1

sidyll
sidyll

Reputation: 59287

If it's a path, don't use a URL. Use a path:

/Users/username/dir

Also, get used to the error parameter, it's really helpful.

NSError *error = nil;
NSDictionary *attr = [fm attributesOfItemAtPath:path error:&error];
if (error)
    NSLog(@"%@", error) /* at least */

It will help you. And if for some reason you don't want it, notice the parameter is a pointer to object, so you may want to use NULL instead of nil.

Upvotes: 7

Related Questions