【问题标题】:Get the latest Created file name in directory获取目录中最新创建的文件名
【发布时间】:2012-03-07 15:01:40
【问题描述】:

我的文件夹包含音频和文本文件

我需要获取最后创建的文本文件和音频文件路径,而不需要在文件夹上循环或不使用可变数组并使用修改后的数据进行排序

任何想法

【问题讨论】:

  • 这个文件夹可以有很多文件的性能

标签: iphone objective-c ipad


【解决方案1】:

有两种解决方案:

  1. 要么您事先没有关于文件夹中文件的完整信息 - 然后您必须循环浏览它们并检查修改日期。
  2. 或者您确实可以控制从一开始就进入该文件夹的所有内容 - 然后您可以保留一个存储最新文件 URL 的首选项。

循环的实现:

NSString *documentsDirectory = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] path];
NSArray *docFileList = [[NSFileManager defaultManager] subpathsAtPath:documentsDirectory];
NSEnumerator *docEnumerator = [docFileList objectEnumerator];
NSString *docFilePath;
NSDate *lastModifiedDate = [NSDate dateWithTimeIntervalSince1970:0];
NSString *lastModifiedFilePath = @"";

while ((docFilePath = [docEnumerator nextObject])) {
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:docFilePath];
    NSDictionary *fileAttributes = [[NSFileManager defaultManager]  attributesOfItemAtPath:fullPath error:nil];
    NSDate currentModifiedDate = [fileAttributes fileModificationDate];

    if (lastModifiedDate < fileModificationDate) {
        fileModificationDate = lastModifiedDate;
        lastModifiedFilePath = fullPath;
    }
}

return lastModifiedFilePath;

【讨论】:

  • 请提供任何示例代码,我无法控制它,我无法执行 perefrence ,因为我还有很多其他文件夹
  • 如何区分 txt 和音频文件对于幼稚的问题很抱歉
  • 查看attributesOfItemAtPath 的文档即可。在这种情况下,使用[fileAttributes fileType]; 或更好,只需使用NSStringpathExtension 检查文件名的最后一个组成部分。也请考虑投票并接受我的回答。
  • 对不起,我没有明白这一点,如果我想在其中搜索的目录在 nsstring 中,请给我帮助
【解决方案2】:

当您下载文件时,您需要管理自动生成的 .plist 文件以及最后一个值获取并证明最新下载文件名的另一个选项。

在这里展示一个教程。 以下代码用于下载音频文件并添加动态创建的 .plist

NSString *strAudioURL=@"Your sound ";

    // check first locally exists or not
    NSString *strPathToAudioCache=[NSString stringWithFormat:@"%@/%@",
                                   [(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],
                                   AudioFolder];

    NSDictionary *dOfAudios=[NSDictionary dictionaryWithContentsOfFile:strPathToAudioCache];

    if([dOfAudios valueForKey:strAudioURL]) {
        [APP_DEL initAndPlayMovie:[NSURL fileURLWithPath:[dOfAudios valueForKey:strAudioURL]] andViewController:self];
    } else {
        NSURL *audioURL = [NSURL URLWithString:strAudioURL];
        NSString *strPathToDownload=[NSString stringWithFormat:@"%@/%@",[(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],
                                     [strAudioURL lastPathComponent]];

        if(!self.rqstForAudio || [self.rqstForAudio isFinished]) {
            self.rqstForAudio=[ASIHTTPRequest requestWithURL:audioURL];
            [self.rqstForAudio setDelegate:self];
            [self.rqstForAudio setAllowResumeForFileDownloads:YES];
            [self.rqstForAudio setCachePolicy:ASIUseDefaultCachePolicy];
            [self.rqstForAudio setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
            [self.rqstForAudio setDidFailSelector:@selector(failedToLoad:)];
            [self.rqstForAudio setDidFinishSelector:@selector(finishedLoading:)];
            [self.rqstForAudio setDownloadCache:[ASIDownloadCache sharedCache]];
            [self.rqstForAudio setDownloadDestinationPath:strPathToDownload];
            [self.rqstForAudio startAsynchronous];
        }

    }

【讨论】:

  • 不知道文件名,只想获取最新修改的音文本文件
  • 嘿,伙计,当您下载名称和值插入到 .plist 文件中的文件时,该名称条目已放入 .plist 文件中,现在您可以访问这些 .plist 数据创建字典并且最后访问文件名
  • 所以首先下载演示然后运行应用程序并显示.plist所在的应用程序的文档目录并打开它,你可以了解它
猜你喜欢
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
相关资源
最近更新 更多