Following is a recursive find files implementation. Does anyone have any ideas on how to further optimize it?
EDIT: No worries, I think this version is about as optimised as it is going to get.
Code:
static HRESULT FindFiles(LPTSTR szPath, BOOL bDeep, LPTSTR pAsterisk)
{
/* FindFiles expects a modifiable string of MAX_PATH length
* with trailing backslash and asterisk. eg. "C:\\my documents\\*" */
HRESULT hr = NOERROR;
HANDLE hFind;
WIN32_FIND_DATA fdata;
hFind = FindFirstFile(szPath, &fdata);
*pAsterisk = TEXT('\0'); /* Remove trailing asterisk */
Gui_UpdateFindPath(szPath);
if (INVALID_HANDLE_VALUE != hFind) {
do {
if (pbj->bCancel) { hr = E_ABORT; break; }
if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
ProcessFile(szPath, &fdata);
}
else if (TEXT('.') != fdata.cFileName[0] && bDeep)
{
HRESULT hrSub;
TCHAR szChild[MAX_PATH];
LPTSTR szEnd;
SIZE_T cchRemaining;
if (SUCCEEDED(StringCchCopyEx(szChild, ARRAYSIZE(szChild), szPath, &szEnd, &cchRemaining, 0)) &&
SUCCEEDED(StringCchCopyEx(szEnd, cchRemaining, fdata.cFileName, &szEnd, &cchRemaining, 0)) &&
SUCCEEDED(StringCchCopyEx(szEnd, cchRemaining, TEXT("\\*"), &szEnd, NULL, 0)))
hrSub = FindFiles(szChild, TRUE, szEnd - 1);
else
hrSub = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
if (FAILED(hrSub) && E_ABORT != hrSub)
ReportError(MSG_ERR_FIND_SUB_DIR, hrSub, ET_WARNING2, __FILE__, __LINE__, szPath, fdata.cFileName);
}
} while (FindNextFile(hFind, &fdata));
FindClose(hFind);
}
else hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
Thanks.