按修改日期降序获取文件夹顺序中的文件列表

时间:2014-04-03 04:00:54

标签: objective-c macos cocoa

我需要获取按创建日期降序排序的数组中的文件列表,即最近修改过的文件。我用NSFileManager检查了几个内置选项。有没有开箱即用的选项?

NSArray *filePathsArray =
[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];

2 个答案:

答案 0 :(得分:3)

请参阅this,可能会有所帮助

//This is reusable method which takes folder path and returns sorted file list
-(NSArray*)getSortedFilesFromFolder: (NSString*)folderPath
{
    NSError *error = nil;
    NSArray* filesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF EndsWith '.pdf'"];//Take only pdf file
    filesArray =  [filesArray filteredArrayUsingPredicate:predicate];

    // sort by creation date
    NSMutableArray* filesAndProperties = [NSMutableArray arrayWithCapacity:[filesArray count]];

    for(NSString* file in filesArray) {

        if (![file isEqualToString:@".DS_Store"]) {
            NSString* filePath = [folderPath stringByAppendingPathComponent:file];
            NSDictionary* properties = [[NSFileManager defaultManager]
                                        attributesOfItemAtPath:filePath
                                        error:&error];
            NSDate* modDate = [properties objectForKey:NSFileModificationDate];

            [filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                           file, @"path",
                                           modDate, @"lastModDate",
                                           nil]];

        }
    }

    // Sort using a block - order inverted as we want latest date first
    NSArray* sortedFiles = [filesAndProperties sortedArrayUsingComparator:
                            ^(id path1, id path2)
                            {
                                // compare
                                NSComparisonResult comp = [[path1 objectForKey:@"lastModDate"] compare:
                                                           [path2 objectForKey:@"lastModDate"]];
                                // invert ordering
                                if (comp == NSOrderedDescending) {
                                    comp = NSOrderedAscending;
                                }
                                else if(comp == NSOrderedAscending){
                                    comp = NSOrderedDescending;
                                }
                                return comp;
                            }];

    return sortedFiles;

}

答案 1 :(得分:0)

这是一个可重用的方法,它接受文件夹路径并返回已排序的文件列表

-(NSArray*)getSortedFilesFromFolder: (NSString*)folderPath
    {
        NSError *error = nil;
        NSArray* filesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF EndsWith '.pdf'"];//Take only pdf file
        filesArray =  [filesArray filteredArrayUsingPredicate:predicate];

        // sort by creation date
        NSMutableArray* filesAndProperties = [NSMutableArray arrayWithCapacity:[filesArray count]];

        for(NSString* file in filesArray) {

            if (![file isEqualToString:@".DS_Store"]) {
                NSString* filePath = [folderPath stringByAppendingPathComponent:file];
                NSDictionary* properties = [[NSFileManager defaultManager]
                                            attributesOfItemAtPath:filePath
                                            error:&error];
                NSDate* modDate = [properties objectForKey:NSFileModificationDate];

                [filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                               file, @"path",
                                               modDate, @"lastModDate",
                                               nil]];

            }
        }

        // Sort using a block - order inverted as we want latest date first
        NSArray* sortedFiles = [filesAndProperties sortedArrayUsingComparator:
                                ^(id path1, id path2)
                                {
                                    // compare
                                    NSComparisonResult comp = [[path1 objectForKey:@"lastModDate"] compare:
                                                               [path2 objectForKey:@"lastModDate"]];
                                    // invert ordering
                                    if (comp == NSOrderedDescending) {
                                        comp = NSOrderedAscending;
                                    }
                                    else if(comp == NSOrderedAscending){
                                        comp = NSOrderedDescending;
                                    }
                                    return comp;
                                }];

        return sortedFiles;

    }