如何在Cocoa中获取包含音乐(mp3)的所有文件夹路径?

发布于 2024-12-06 18:14:57 字数 1161 浏览 0 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔姬 2024-12-13 18:14:57

如果我错了请纠正我。您想要获取文件夹中 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文