views:

1385

answers:

3

Is there an method to get the contents of a folder in a particular order? I'd like an array of file attribute dictionaries (or just file names) ordered by date modified.

Right now, I'm doing it this way:

  • get an array with the file names
  • get the attributes of each file
  • store the file's path and modified date in a dictionary with the date as a key

Next I have to output the dictionary in date order, but I wondered if there's an easier way? If not, is there a code snippet somewhere which will do this for me?

Thanks.

+1  A: 

Here's an example of how to do it. This approach gets an array of files from the NSFileManager, then sorts that array based on the NSURL property NSURLContentModificationDateKey.

I missed the iPhone tag the first time. I've changed the code to work with the iPhone SDK.

NSInteger lastModifiedSort(id path1, id path2, void* context)
{
    return [[path1 objectForKey:@"lastModDate"] compare:
            [path2 objectForKey:@"lastModDate"]];
}

-(void)filesByModDate
{
    NSString* path = @"/";
    NSError* error = nil;

    NSArray* files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path
                                                                         error:&error];
    if(error == nil)
    {
        NSMutableArray* filesAndProperties = [NSMutableDictionary
                                       dictionaryWithCapacity:[files count]];
        for(NSString* path in files)
        {
            NSDictionary* properties = [[NSFileManager defaultManager]
                                        attributesOfItemAtPath:NSFileModificationDate
                                        error:&error];
            NSDate* modDate = [properties objectForKey:NSFileModificationDate];

            if(error == nil)
            {
                [filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                               path, @"path",
                                                modDate, @"lastModDate",
                                               nil]];                 
            }
        }

        NSArray* sortedFiles = [files sortedArrayUsingFunction:&lastModifiedSort
                                                       context:nil];

        // SUCCESS!!
        NSLog(@"sortedFiles: %@", sortedFiles);            
    }
    else
    {
        NSLog(@"Encountered error while accessing contents of %@: %@", path, error);
    }
}
nall
Thanks for the answer. There seems to be a few things that won't work with the iPhone SDK here (NSURLContentModificationDateKey and some other NSURL methods).
nevan
I've started trying to change it to work with the iPhone SDK, but getResourceValue doesn't exist there. I'll look for an array sort for iPhone.
nevan
nevan, I updated this to work within the constraints of the iPhone SDK
nall
A: 

It's too slow

[[NSFileManager defaultManager] attributesOfItemAtPath:NSFileModificationDate error:&error];

Try this code:

+ (NSDate*) getModificationDateForFileAtPath:(NSString*)path{
struct tm* date;                    // create a time structure
struct stat attrib;                 // create a file attribute structure

stat([path UTF8String], &attrib);   // get the attributes of afile.txt

date = gmtime(&(attrib.st_mtime));  // Get the last modified time and put it into the time structure

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setSecond:   date->tm_sec];
[comps setMinute:   date->tm_min];
[comps setHour:     date->tm_hour];
[comps setDay:      date->tm_mday];
[comps setMonth:    date->tm_mon + 1];
[comps setYear:     date->tm_year + 1900];

NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *modificationDate = [[cal dateFromComponents:comps] addTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT]];

[comps release];

return modificationDate;

}

abuharsky
+2  A: 
M-Y