目錄

2012-02-16 25 views
0

獲取最新創建的文件名,我有文件夾包含音頻和文本文件目錄

我需要得到最後創建的文本文件和音頻文件的路徑不循環的文件夾或沒有使用可變數組和順序使用修改數據

任何想法

+0

爲什麼你不想循環? – Mundi 2012-02-16 07:55:33

+0

這個文件夾可能會有很多文件 – AMH 2012-02-16 08:02:42

回答

3

解決辦法有兩個:

  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; 
+0

的任何示例代碼的性能,我沒有它的控制和我不能做perefrence,因爲我有很多其他文件夾 – AMH 2012-02-16 08:04:20

+0

如果你可以給示例代碼,將是很大 – AMH 2012-02-16 08:09:44

+0

我添加一些代碼爲您提供方便。 – Mundi 2012-02-16 08:28:01

0

另一個選項爲當你下載文件到時候你就需要管理,其中自動生成該文件的.plist和最後一個值取並證明了最新下載文件名。

在這裏顯示一個教程。 以下代碼用於下載音頻文件並添加動態創建的.plist

NSString *[email protected]"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]; 
     } 

    } 
+0

我不知道文件名,我只想得到最新修改的音頻和文本文件 – AMH 2012-02-16 08:16:51

+0

嘿哥們,當你下載文件的名字和價值插入到.plist文件,從而該名稱條目放入.plist文件,現在你可以訪問這些.plist數據創建字典和訪問姓氏文件 – 2012-02-16 08:33:35

+0

,所以首先下載演示,然後運行應用程序,並顯示一個.plist所在位置的應用程序的文檔目錄,然後打開它,並且可以瞭解它 – 2012-02-16 08:33:46