2011-09-26 78 views
1

我是Cocoa的新手。我試圖在我的應用程序中掃描計算機上的音樂文件夾。但我有一些邏輯。 我需要從/ Users開始掃描,例如,如果我有/ Users/music/anotherMusic和/ Users/music/music2/music3,我需要只能獲得包含音樂(/用戶/音樂)。還需要繼續在子文件夾中迭代以在根文件夾(/ Users/music)中計算音樂文件。等用同樣的邏輯繼續掃描整個computer.And結果應該顯示在表格中,例如結果應該是這樣的如何獲取可可中包含音樂(mp3)的所有文件夾路徑?

- - /用戶/我的電腦/ iTunes的 - 77個文件夾,500個的音樂文件

- /用戶/我的電腦/文件/音樂-15音樂文件夾,400個的音樂文件

- /用戶/我的電腦/下載/媒體-20音樂文件夾,325個音樂
文件(的數所有蘇有音樂的b目錄)

我該怎麼做?我做了什麼,它在下面的代碼中。但是它一直在深入,給我每條包含.mp3音樂的路徑,但我需要上面提到的邏輯。

NSFileManager *manager = [NSFileManager defaultManager]; 

     NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:@"/Users/"];     
     NSString * file; 
     while((file=[direnum nextObject])) 
     { 
      NSString * FullPath=[NSString stringWithFormat:@"/Users/%@",file]; 

      if ([file hasSuffix:@".mp3"]) 
     { 
      ///gives a music file path that contains mp3, or I can cut last part, and get the folder path. But no logic as should be. 
     } 
    } 

任何想法,或幫助讚賞。謝謝。

回答

3

糾正我,如果我錯了。您想獲取文件夾中的mp3數量,但是當文件夾只包含文件夾和mp3文件時,mp3數量是針對其父文件夾的(如果父本身僅包含文件夾和mp3,父母等)對不對?

[manager enumeratorAtPath]非常有用,但在這種情況下,它肯定會要求您維護一個堆棧以跟蹤您瀏覽的文件。

遞歸是幸福的,在這裏。

- (BOOL)isMp3File:(NSString*)file 
{ 
    return [file hasSuffix:@".mp3"]; 
} 

- (BOOL)isHiddenFile:(NSString*)file 
{ 
    return [file hasPrefix:@"."]; 
} 

- (void)parseForMp3:(NSMutableDictionary*)dic 
      inPath:(NSString*)currentPath 
      forFolder:(NSString*)folder 
{ 
    BOOL comboBreak = false; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError* error; 
    NSArray* files = [fileManager contentsOfDirectoryAtPath:currentPath error:&error]; 
    if (error) 
    { 
     //throw error or anything 
    } 
    for (NSString *file in files) 
    { 
     BOOL isDirectory = false; 
     NSString *fullPath = [NSString stringWithFormat:@"%@/%@", currentPath, file]; 
     [fileManager fileExistsAtPath:fullPath isDirectory:&isDirectory]; 
     comboBreak = comboBreak || (!isDirectory && 
            ![self isMp3File:file] && 
            ![self isHiddenFile:file]); 
     if (comboBreak) 
      break; 
    } 
    for (NSString *file in files) 
    { 
     BOOL isDirectory = false; 
     NSString *fullPath = [NSString stringWithFormat:@"%@/%@", currentPath, file]; 
     [fileManager fileExistsAtPath:fullPath isDirectory:&isDirectory]; 
     if (isDirectory) 
     { 
      if (!comboBreak) 
      { 
       [self parseForMp3:dic inPath:fullPath forFolder:folder]; 
      } 
      else 
      { 
       [self parseForMp3:dic inPath:fullPath forFolder:fullPath]; 
      } 
     } 
     else if ([self isMp3File:file]) 
     { 
      NSNumber *oldValue = [dic valueForKey:folder]; 
      oldValue = [NSNumber numberWithUnsignedInteger:[oldValue unsignedIntegerValue] + 1]; 
      [dic setValue:oldValue forKey:folder]; 
     } 
    } 
} 


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    // Insert code here to initialize your application 
    NSMutableDictionary *dic = [NSMutableDictionary dictionary]; 
    [self parseForMp3:dic inPath:@"/Users/panosbaroudjian" forFolder:@"/Users/panosbaroudjian"]; 
    NSLog(@"%@", dic); 
} 
+0

謝謝它非常好,非常快!但有一個問題我不明白。它按照字母順序掃描,如果你有文件夾/用戶/音樂/音樂2 /音樂3,它可以很好地顯示/用戶/音樂。但是如果你有/用戶/音樂和音樂歌曲,以字母'A'或任何大'm'開頭,結果將是/ Users/music和/ Users/music/music2,但它應該只是父目錄包含音樂或音樂的子文件夾。我如何解決它?非常感謝 – User1234

+0

確實有一點小錯誤。在瀏覽之前,您必須檢查文件列表以檢查comboBreak布爾值。我會編輯它。 – Panos

+0

非常感謝Panos。我在編輯後試過,但仍然是同樣的問題。看看我得到「/用戶/蘋果/桌面/ w/a /音樂」= 16; 「/ Users/Apple/Desktop/w/a/music/d」= 3; 「/ Users/Apple/Desktop/w/a/music/d/aa/a/asdfasdf/untitled folder/a」= 1; 「/ Users/Apple/Desktop/w/aaa」= 1;但它應該只有「/用戶/蘋果/桌面/ W」它有時很好,如果按字母順序是正確的。我試圖解決它,但我cound't,我感謝您的幫助。 – User1234

相關問題