【问题标题】:Get files list in a folder order by Modified date desending按修改日期降序获取文件夹顺序的文件列表
【发布时间】:2014-05-14 16:33:24
【问题描述】:

我需要获取按创建日期降序排序的数组中的文件列表,即顶部最近修改的文件。我用NSFileManager 检查了几个内置选项。是否有现成可用的选项?

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

【问题讨论】:

  • 看到这个pastebin.com/xt6JutrQ,可能会有帮助
  • 谢谢。只需稍加修改,我就能完成这项工作。
  • @zaheer,请添加您的评论作为答案。这样你的答案就会被接受。

标签: objective-c macos cocoa


【解决方案1】:

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;

}

【讨论】:

  • 如果DS_Store 只过滤了带有 pdf 扩展名的文件,则不需要检查它。此外,为了更好的可重用性,我会将扩展添加为方法参数。还有ENDSWITH[c]接受PDF和pdf。
【解决方案2】:

这是一种可重用的方法,它采用文件夹路径并返回排序的文件列表

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

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多