05-15-2001 02:00 AM
05-16-2001 01:56 PM
05-22-2001 05:30 AM
01-16-2008 12:43 PM
06-17-2009 05:45 PM
I know this is an old thread, but I created a recursive folder search routine based on the ideas here, and I thought someone else might like to have it. My code is designed to iterate through a set of folders. When it finds a new folder, it calls a passed-in function pointer to process the folder. This way the recursion part is separated from the processing part.
typedef void (*SubFolderProcessor)(const char *const SubFolder);
static void IterateSubFolders(char *Path, SubFolderProcessor);
/*********************/
/* IterateSubFolders */
/*********************/
static void IterateSubFolders(char *Path, SubFolderProcessor FolderProcessor)
{
int NumberOfFileMatches = 0;
int FileWasFound;
char SearchString[MAX_PATHNAME_LEN];
char FoundFile[MAX_PATHNAME_LEN];
char FullPathToFoundFile[MAX_PATHNAME_LEN];
// create search string
strcpy(SearchString, Path);
strcat(SearchString, "\\*.*");
// look for files
FileWasFound = GetFirstFile (SearchString, 1, 0, 0, 0, 0, 1, FoundFile);
// if we found something (note: here 0 means YES)...
while (FileWasFound == 0)
{
int IsSubFolder;
int junk1, junk2, junk3, junk4;
// determine if it's a folder or a file
sprintf(FullPathToFoundFile, "%s\\%s", Path, FoundFile);
IsSubFolder = GetFileAttrs (FullPathToFoundFile, &junk1, &junk2, &junk3, &junk4);
// if it is a folder...
if (IsSubFolder)
{
int FileSearchMatches;
// process the new folder
FolderProcessor(FullPathToFoundFile);
////////////////////
// RECURSIVE CALL //
////////////////////
// iterate the new folder
IterateSubFolders(FullPathToFoundFile, FolderProcessor);
/*
The recursive call reset the GetFirstFile/GetNextFile mechanism,
so we have to restart it for the parent path and then move past
all the files/folders we've already examined.
*/
FileWasFound = GetFirstFile (SearchString, 1, 0, 0, 0, 0, 1, FoundFile);
for (FileSearchMatches = 0; FileSearchMatches < NumberOfFileMatches; ++FileSearchMatches)
{
GetNextFile(FoundFile);
} /* for */
} /* if-then */
// increment the number of files/folders we've found
++NumberOfFileMatches;
// get the next matching file/folder
FileWasFound = GetNextFile(FoundFile);
} /* if-then */
} /* IterateSubFolders */
Here's an example of how to use it:
/***********************/
/* ProcessNewSubFolder */
/***********************/
static void ProcessNewSubFolder(const char *const SubFolder)
{
InsertTestBoxLine(panelHandle, PANEL_txtFolderList, -1, SubFolder);
} /* ProcessNewSubFolder */
...
ResetTextBox(panelHandle, PANEL_txtFolderList, "");
IterateSubFolders("C:", ProcessNewSubFolder);...
06-22-2009 02:47 PM
This thread seems to have resurfaced, You could use the FileSelectPopup() routine from the user interface library, it works great, you can also use the GetDir, GetProjectDir, or GetFullPathFromProject functions before calling the fileselectpopups. You can tell the popup which folder to open with by default then the user can search all folders like a regular windows save/open window. If you need more help using the functions I described, I'd be happy to help. Hopefully this helps, good luck!
Cheers!
Doug