programmer2002
Programmer
Hi gurus,
I adapted codes from one member of this forum for my application with the following function.
I try to search for files with special extension recursively in a particular directory.
In the function, the program should chdir to 'root'. Then scan the folder for files. If the file is a directory, then recursively search for files in the directory.
But when recursively calling the function, the directory is not changed into 'root' (chdir((char *)(LPCTSTR)root). That means the program all the way searches for the files in the same directory, it never goes down into subdirectories.
Anybody can help me on this? It's really appreciated.
Regards.
int CTutPanel::findTutorials(CTutFile* fileGroup, CString root)
{
CFileFind finder;
CString fileName;
static int count;
//find files in a folder
chdir((char *)(LPCTSTR)root);
BOOL bWorking = finder.FindFile(_T("*.*");
while (bWorking && (root.CompareNoCase((LPCTSTR)m_path) != 0))
{
bWorking = finder.FindNextFile();
fileName = finder.GetFileName();
if(finder.IsDirectory() || (fileName.Find(_T(".Tut") == -1)) continue;
if(fileName.Find(_T(".Tut") != -1){
fileGroup->tutFileName = fileName;
count++;
}
else if(fileName.Find(_T(".ico") != -1 || fileName.Find(_T(".bmp") != -1) fileGroup->iconFileName = fileName;
}
//chdir if FindFile returns a directory
BOOL traverse = finder.FindFile(_T("*.*");
while(traverse)
{
traverse = finder.FindNextFile();
CString tmp = finder.GetFileName();
if(tmp != "." && tmp != ".."
{
if(finder.IsDirectory())
{
CTutFile* temp = new CTutFile();
fileGroup->nextFile = temp;
findTutorials(temp, finder.GetFilePath());
}
}
}
return count;
}
I adapted codes from one member of this forum for my application with the following function.
I try to search for files with special extension recursively in a particular directory.
In the function, the program should chdir to 'root'. Then scan the folder for files. If the file is a directory, then recursively search for files in the directory.
But when recursively calling the function, the directory is not changed into 'root' (chdir((char *)(LPCTSTR)root). That means the program all the way searches for the files in the same directory, it never goes down into subdirectories.
Anybody can help me on this? It's really appreciated.
Regards.
int CTutPanel::findTutorials(CTutFile* fileGroup, CString root)
{
CFileFind finder;
CString fileName;
static int count;
//find files in a folder
chdir((char *)(LPCTSTR)root);
BOOL bWorking = finder.FindFile(_T("*.*");
while (bWorking && (root.CompareNoCase((LPCTSTR)m_path) != 0))
{
bWorking = finder.FindNextFile();
fileName = finder.GetFileName();
if(finder.IsDirectory() || (fileName.Find(_T(".Tut") == -1)) continue;
if(fileName.Find(_T(".Tut") != -1){
fileGroup->tutFileName = fileName;
count++;
}
else if(fileName.Find(_T(".ico") != -1 || fileName.Find(_T(".bmp") != -1) fileGroup->iconFileName = fileName;
}
//chdir if FindFile returns a directory
BOOL traverse = finder.FindFile(_T("*.*");
while(traverse)
{
traverse = finder.FindNextFile();
CString tmp = finder.GetFileName();
if(tmp != "." && tmp != ".."
{
if(finder.IsDirectory())
{
CTutFile* temp = new CTutFile();
fileGroup->nextFile = temp;
findTutorials(temp, finder.GetFilePath());
}
}
}
return count;
}