There are a couple of ways to find/search files in a folder/directory. Usually, Win32 programmers use FindFirstFile & FindNextFile functions to find the files in a particular folder. MFC provides a simplified way of finding the files using CFileFind class.
The following example shows how to find files in a particular folder. This example is also helpful to find files recursively:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void FileFind(CString sPath) | |
{ | |
CFileFind finder; | |
BOOL bFind = finder.FindFile(sPath); | |
while ( bFind ) | |
{ | |
bFind = finder.FindNextFileW(); | |
if ( finder.IsDots() ) | |
continue; | |
if ( finder.IsDirectory() ) | |
{ | |
wprintf(_T("[%s]\n"), finder.GetFileName()); | |
FileFind(finder.GetFilePath() + _T("\\*.*")); | |
} | |
else | |
wprintf(_T("%s\n"), finder.GetFileName()); | |
} | |
finder.Close(); | |
} |
You can call the above function anywhere in your program. For example, the following function call shows all the files in C: drive recursively:
FileFind(_T("C:\\*.*"));
—
Finding files in a folder / directory using MFC!
One thought on “Finding files in a folder / directory using MFC!”