Recursively Searching Folders
I''m doing me a little mp3 player using FMOD. Right now I''m using FindFirstFile... to get my list of mp3s. The only problem is it doesn''t search folders recursively. Is there a way to modify the use of the find file functions to get directorys? If you use the *.* as the search then it will pickup all the subfolders. Or is there some other way to search?
Tried just to search the fourms for an answer but as usual the search function is down.
Assuming C++, I''d just use boost::filesystem. It''s fairly easy to apply recursive searches using directory iterators.
I''m trying to keep it straight C for now. The WIN32_FIND_DATA structure a attributes flag that will tell you if the returned name is a file or folder. I assume that my search function has to be recursive itself. So if the attribute does happen to be for a folder I call the function again. But when it gets called the second time the program crashes.
Found something kind of odd with this. I''m using just a standard FindFirstFile, FindNextFile loop. After I had it find the first folder (which always seems to be ''.'') I was calling the function again and it would crash. I took that call out and left the call in the actual loop and it searches all the folders. I tested it to six deep but it still found the file. Does anybody have an explaination?
Why don''t you post the code for your function?
Oh, and . and .. are special directory names. "." refers to the current directory and ".." refers to the parent directory.
Oh, and . and .. are special directory names. "." refers to the current directory and ".." refers to the parent directory.
Actually it doesn''t work right, I can only find files that are one folder deep.
Sorry that its not commented but it should be pretty easy to figure out what I''m doing. When I try to call the function again is when I get problems. I leave that out then it works, but only one folder deep.
void SearchFolder(char *Folder){ WIN32_FIND_DATA FindFileData; HANDLE hFind; char SearchPath[MAX_PATH] = ""; char SubFolder[MAX_PATH] = ""; char temp[MAX_PATH]; strcpy(SearchPath, Folder); strcat(SearchPath, "\\*.*"); hFind = FindFirstFile(SearchPath, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { //MessageBox(g_hwnd, "No valid music files in this folder!", "EH MP3", MB_OK); return; } FindMusicFiles(Folder); if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { strcpy(temp, Folder); strcat(temp, "\\"); strcat(temp, FindFileData.cFileName); SearchFolder(temp); } while (FindNextFile(hFind, &FindFileData)) { if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { strcpy(temp, Folder); strcat(temp, "\\"); strcat(temp, FindFileData.cFileName); SearchFolder(temp); } } FindClose(hFind);}void FindMusicFiles(char *Folder){ WIN32_FIND_DATA FindFileData; HANDLE hFind; char SearchPath[MAX_PATH] = ""; char SubFolder[MAX_PATH] = ""; strcpy(SearchPath, Folder); strcat(SearchPath, "\\*.mp3"); hFind = FindFirstFile(SearchPath, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { //MessageBox(g_hwnd, "No valid music files in this folder!", "EH MP3", MB_OK); return; } SendMessage(GetDlgItem(g_hwnd, IDC_FILELIST), LB_ADDSTRING, 0, (LPARAM)FindFileData.cFileName); while (FindNextFile(hFind, &FindFileData)) { SendMessage(GetDlgItem(g_hwnd, IDC_FILELIST), LB_ADDSTRING, 0, (LPARAM)FindFileData.cFileName); } FindClose(hFind);}
Sorry that its not commented but it should be pretty easy to figure out what I''m doing. When I try to call the function again is when I get problems. I leave that out then it works, but only one folder deep.
Remember that "." and ".." directories are returned too. Look for them and don''t recurse into them.
I realized that "." and ".." were my problem when I was stepping through the function. It was basically going for the root of the selected folder instead of its subfolders.
Seems to have fixed the problem.
if ((strcmp(FindFileData.cFileName,".") != 0) && (strcmp(FindFileData.cFileName, "..") != 0)) SearchFolder(temp);
Seems to have fixed the problem.
Checkout the FindFileContainer.h listing on this page:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20993600.html
It works very well for me.
[edited by - toddhd on June 10, 2004 1:24:14 PM]
[edited by - toddhd on June 10, 2004 1:24:35 PM]
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20993600.html
It works very well for me.
[edited by - toddhd on June 10, 2004 1:24:14 PM]
[edited by - toddhd on June 10, 2004 1:24:35 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement