Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

darkchrono4

Recursively Searching Folders

This topic is 5276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Actually it doesn''t work right, I can only find files that are 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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Remember that "." and ".." directories are returned too. Look for them and don''t recurse into them.

Share this post


Link to post
Share on other sites
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.


if ((strcmp(FindFileData.cFileName,".") != 0) &&
(strcmp(FindFileData.cFileName, "..") != 0))
SearchFolder(temp);


Seems to have fixed the problem.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!