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.