如何获取包含音乐(mp3)在Cocoa中的所有文件夹路径? [英] How to get all folders paths that contains music (mp3) in Cocoa?

查看:214
本文介绍了如何获取包含音乐(mp3)在Cocoa中的所有文件夹路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



- / Users / myComputer / Downloads / 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])
{
///给出包含mp3的音乐文件路径,或者我可以剪切最后一部分,获取文件夹路径。但没有逻辑应该是。
}
}

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

解决方案

如果我错了,请纠正我。你想得到你的文件夹中的mp3数,但是一个文件夹只包含文件夹和mp3文件,mp3计数是其父文件夹(如果父本身只包含文件夹和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 *)文件夹
{
BOOL comboBreak = false;
NSFileManager * fileManager = [NSFileManager defaultManager];
NSError * error;
NSArray * files = [fileManager contentOfDirectoryAtPath:currentPath error:& error];
if(错误)
{
//抛出错误或任何
}
(文件中的NSString *文件)
{
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代码在这里初始化你的应用程序
NSMutableDictionary * dic = [NSMutableDictionary dictionary];
[self parseForMp3:dic inPath:@/ Users / panosbaroudjianforFolder:@/ Users / panosbaroudjian];
NSLog(@%@,dic);
}


I'm new in Cocoa.I'm trying to scan computer for music folders,within my application. But I have some logic for it. What I need to start scaning from /Users , for example if I have /Users/music/anotherMusic and /Users/music/music2/music3 , I need to be able to get only the top for that folders that contains music (/Users/music ). And also need to continue iterate in subfolders to have a count of music files in the root folder (/Users/music). And so on with the same logic continue scanning whole computer.And results should be displayed in table, for example RESULTS should be like this

- -/Users/myComputer/iTunes - 77 folders, 500 music files

- /Users/myComputer/Documents/Music -15 music folders,400 music files

- /Users/myComputer/Downloads/Media -20 music folders, 325 music
files (count of all subdirectories with music)

How I can do it? What I did ,it is in the code below. But it's going deep all the way down, and give me each path that contains .mp3 music, but I need logic as mentioned above.

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.
        }
     }

Any ideas, or help appreciated. Thanks.

解决方案

Correct me if I am wrong. You want to get the number of mp3 in your folder, but while a folder only contains folders and mp3 files, the mp3-count is for its parent folder (and if the parent itself only contains folders and mp3s, it counts for its grand-parent, etc.) right ?

[manager enumeratorAtPath] is quite useful, but in this case it will certainly require you to maintain a stack to keep track of your browsed files.

Recursion is bliss, here.

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

这篇关于如何获取包含音乐(mp3)在Cocoa中的所有文件夹路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆