This is a basic routine that works.
BOOL rd_cb(LPTSTR lpszFile, PCTSTR pszExtension)
{
PCTSTR cur_ext = PathFindExtension(lpszFile);
if (cur_ext && _tcscmp(cur_ext, pszExtension) == 0)
{
wcout << "Found: " << lpszFile << endl;
return TRUE;
}
return FALSE;
}
int RecursiveDirectory(PCTSTR lpszPath, PCTSTR pszExtension)
{
WIN32_FIND_DATA WFD;
int qwCount = 0;
TCHAR szFileSpec[MAX_PATH + 1];
PathCombine(szFileSpec, lpszPath, TEXT("*.*"));
HANDLE hSearch = FindFirstFile(szFileSpec, &WFD);
if (hSearch == INVALID_HANDLE_VALUE) return 0;
do
{
if (_tcscmp(WFD.cFileName, TEXT("..")) && _tcscmp(WFD.cFileName, TEXT(".")))
{
if (WFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && !(WFD.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
{
PathCombine(szFileSpec, lpszPath, WFD.cFileName);
qwCount += RecursiveDirectory(szFileSpec, pszExtension);
}
else
{
PathCombine(szFileSpec, lpszPath, WFD.cFileName);
if (rd_cb(szFileSpec, pszExtension))
qwCount++;
}
}
} while (FindNextFile(hSearch, &WFD));
FindClose(hSearch);
return qwCount;
}
The initial call would be something like:
RecursiveDirectory(TEXT("C:\\Users\\username\\Documents"), TEXT(".txt"));
[edit]
Replaced the lpCallback with a straightforward function call, to simplify the code.
[/edit]