【问题标题】:How to get all folders paths that contains music (mp3) in Cocoa?如何在 Cocoa 中获取所有包含音乐(mp3)的文件夹路径?
【发布时间】:2011-09-26 20:12:52
【问题描述】:

我是 Cocoa 的新手。我正在尝试在我的应用程序中扫描计算机中的音乐文件夹。但我有一些逻辑。 我需要从 /Users 开始扫描,例如,如果我有 /Users/music/anotherMusic 和 /Users/music/music2/music3 ,我需要能够只获取包含音乐的文件夹的顶部(/Users /音乐 )。并且还需要继续迭代子文件夹以计算根文件夹(/Users/music)中的音乐文件。以同样的逻辑继续扫描整个计算机。结果应该显示在表格中,例如RESULTS应该是这样的

- -/Users/myComputer/iTunes - 77 个文件夹,500 个音乐文件

- /Users/myComputer/Documents/Music -15 个音乐文件夹,400 个音乐文件

- /Users/myComputer/Downloads/Media -20 个音乐文件夹,325 个音乐
文件(包含音乐的所有子目录的计数)

我该怎么做?我所做的,它在下面的代码中。但它一直深入下去,给我每条包含 .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.
        }
     }

任何想法或帮助表示赞赏。谢谢。

【问题讨论】:

    标签: objective-c macos cocoa enumeration


    【解决方案1】:

    如果我错了,请纠正我。您想获取文件夹中 mp3 的数量,但是虽然一个文件夹仅包含文件夹和 mp3 文件,但 mp3-count 是针对其父文件夹(如果父文件夹本身仅包含文件夹和 mp3,则计入其 Grand-父母等)对吗?

    [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);
    }
    

    【讨论】:

    • 谢谢它工作得很好而且非常快!但是有一个问题我想不通。它按字母顺序扫描,如果您有文件夹 /Users/music/music2/music3 它工作正常,它会显示 /Users/music 。但是,如果您有 /Users/music 和以字母“A”或任何更大的“m”开头的音乐歌曲,结果将是 /Users/music 和 /Users/music/music2 ,但它应该只是父目录包含音乐或音乐的子文件夹。我该如何解决?非常感谢
    • 确实有点小错误。您必须在浏览之前检查一次文件列表以检查 comboBreak 布尔值。我会编辑它。
    • 非常感谢帕诺斯。我在您的编辑后尝试过,但仍然是同样的问题。看看我得到了什么 "/Users/Apple/Desktop/w/a/music" = 16; "/Users/Apple/Desktop/w/a/music/d" = 3; "/Users/Apple/Desktop/w/a/music/d/aa/a/asdfasdf/untitled 文件夹/a" = 1; “/用户/苹果/桌面/w/aaa”= 1;但它应该只是“/Users/Apple/Desktop/w”,如果字母顺序正确,它有时可以正常工作。我试图修复它,但我不能,感谢您的帮助。
    • 您确定不是因为 .DS_STORE 文件或其他原因吗?我能够重现您的错误的唯一方法是当一个文件夹包含一个隐藏文件(如 .DS_STORE)时,该文件的组合会破坏递归(我们确实不希望这样)。我将编辑答案以检查隐藏文件(并忽略它们)。
    • 感谢您帮助帕诺斯。重现的方式,例如在桌面上创建文件夹。放一个音乐文件,例如以“R”开头和两个包含音乐的文件夹,按字母顺序命名一个少“R”的文件夹,一个多“R”的文件夹,并在其中放入一些音乐,然后尝试运行您将看到的程序。帕诺斯你是亚美尼亚人吗?
    猜你喜欢
    • 2017-01-16
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多