Get directory contents in date modified order

后端 未结 5 2213
盖世英雄少女心
盖世英雄少女心 2020-12-04 14:19

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.

5条回答
  •  孤街浪徒
    2020-12-04 15:19

    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;
    }
    

提交回复
热议问题