user1321964
user1321964

Reputation: 35

Reading fileModificationDate

How do I access the fileModificationDate

    NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:
                         @"TempiPad"];
    NSFileManager *localFileManager=[[NSFileManager alloc] init];
    NSDirectoryEnumerator *dirEnum =
    [localFileManager enumeratorAtPath:docsDir];
    NSString *file;

    // List all files in docsDir
    while (file = [dirEnum nextObject]) {
        NSLog(@"File = %@",file);
        NSError *attributesError = nil;
        NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:file error:&attributesError];

        NSString *fileModificationDate = [fileAttributes objectForKey:NSFileModificationDate];
        NSLog(@"ModDate = %@",fileModificationDate);

        } 
    }

This just outputs (null). Eventually I will want to compare the fileModificationDate for two files, so how will I do that?

Upvotes: 0

Views: 917

Answers (2)

Wevah
Wevah

Reputation: 28242

IIRC, you need to append the filename to the directory path; something like:

while (file = [dirEnum nextObject]) {
    NSLog(@"File = %@",file);
    NSError *attributesError = nil;
    file = [docsDir stringByAppendingPathComponent:file];
    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:file error:&attributesError];

    NSDate *fileModificationDate = [fileAttributes objectForKey:NSFileModificationDate];
    NSLog(@"ModDate = %@",fileModificationDate);

    } 
}

And, as more tension said, NSFileModificationDate is an NSDate, not an NSString.

Upvotes: 2

more tension
more tension

Reputation: 3352

NSFileModificationDate returns an NSDate instance, not an NSString. See:

NSFileModificationDate

Upvotes: 0

Related Questions