我编写了一个代码来遍历驱动器以查找特定扩展名的文件,如果文件是fount,它将被添加到列表框中,但代码在找到目录时失败了。迭代停止,文件不是目录。我希望我的程序使用给定的扩展名搜索文件,即使在目录和子目录中,我该怎么办?
这是我的代码
我的代码中看到的变量缓冲区只不过是驱动器字符串,例如H:\ count = 0;
int Class::countOfDocuments(wchar_t buffer[10])
{
wchar_t driveString[MAX_PATH + 1] = { 0 };
wcsncat_s(driveString,260,buffer,260);
wcsncat_s(driveString, 260, L"*doc", 260);
WIN32_FIND_DATA documents;
HANDLE hFind; bool var = true;
hFind = FindFirstFile(driveString, &documents);
if (INVALID_HANDLE_VALUE == hFind)
{
wchar_t* no = L"No documents found";
SendMessage(list1, LB_ADDSTRING, NULL, (LPARAM)no);
for (int i = 0; i <= 10; i++)
{
SendMessage(cprogress, PBM_SETRANGE, 0, i);
SendMessage(cprogress, PBM_SETSTEP, (WPARAM)1, 0);
SendMessage(cprogress, PBM_STEPIT, 0, 1);
}
return count = -1;
}
else
{
wchar_t* Yes = L"Document found";
perform = true;
SendMessage(list1, LB_ADDSTRING, NULL, (LPARAM)Yes);
while (var = FindNextFile(hFind, &documents) == TRUE)
{
count++;
}
for (int i = 0; i <= count; i++)
{
SendMessage(cprogress, PBM_SETRANGE, 0, i);
SendMessage(cprogress, PBM_SETSTEP, (WPARAM)1, 0);
SendMessage(cprogress, PBM_STEPIT, 0, 1);
wchar_t* doc = L"Document found";
SendMessage(list1, LB_ADDSTRING, NULL, (LPARAM)doc);
}
wchar_t* z = L"search complete";
SendMessage(list1, LB_ADDSTRING, NULL, (LPARAM)z);
}
return count;
}