如何在Cocoa中获取包含音乐(mp3)的所有文件夹路径?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我错了请纠正我。您想要获取文件夹中 mp3 的数量,但是虽然文件夹仅包含文件夹和 mp3 文件,但 mp3 计数是其父文件夹的数量(如果父文件夹本身仅包含文件夹和 mp3,则它会计算其总文件夹数)父母等)对吗?
[manager enumeratorAtPath] 非常有用,但在这种情况下,它肯定需要您维护一个堆栈来跟踪您浏览的文件。
在这里,递归是幸福的。
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.