Вам необходимо определить, является ли текущий каталог пустым, если он пуст, поместите каталог в вектор файла, если нет, введите каталог и продолжайте поиск.
bool crawldirs(wstring path, wstring mask, vector<wstring>& files) {
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA ffd;
wstring spec;
stack<wstring> directories;
directories.push(path);
files.clear();
while (!directories.empty()) {
path = directories.top();
spec = path + L"\\" + mask;
directories.pop();
BOOL isEmpty = true;//determine if it's an empty folder.
hFind = FindFirstFile(spec.c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
return false;
}
do {
if (wcscmp(ffd.cFileName, L".") != 0 &&
wcscmp(ffd.cFileName, L"..") != 0) {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
directories.push(path + L"\\" + ffd.cFileName);
}
else {
files.push_back(path + L"\\" + ffd.cFileName);
isEmpty = false;//It not an empty folder since it has normal file.
}
}
} while (FindNextFile(hFind, &ffd) != 0);
if (isEmpty)
{
files.push_back(path);
}
if (GetLastError() != ERROR_NO_MORE_FILES) {
FindClose(hFind);
return false;
}
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
return true;
}
Или вместо цикла while. Более четко использовать рекурсивный вызов для получения списка всех каталогов, подкаталогов и файлов.
bool RetrieveFile(TCHAR* szDir)
{
BOOL isEmpty = true;
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
_tcscat(szDir, _T("\\*"));
HANDLE hFind = FindFirstFile(szDir, &ffd);
// List all the files in the directory with some info about them.
if (hFind == INVALID_HANDLE_VALUE) {
return false;
}
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (!_tcscmp(ffd.cFileName, _T(".")) || !_tcscmp(ffd.cFileName, _T("..")))
continue;
TCHAR szDircpy[MAX_PATH] = { 0 };
_tcscpy(szDircpy, szDir);
szDircpy[_tcslen(szDircpy) - 1] = _T('\0'); // remove "*" character from "xxx\\*"
_tcscat(szDircpy, ffd.cFileName); //add the subdirectory name into szDir
RetrieveFile(szDircpy);
}
else
{
isEmpty = false;
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
TCHAR szfilepath[MAX_PATH] = { 0 };
_tcscpy(szfilepath, szDir);
szfilepath[_tcslen(szfilepath) - 1] = _T('\0'); // remove "*" character from "xxx\\*"
_tcscat(szfilepath, ffd.cFileName); //add the subdirectory name into szDir
_tprintf(TEXT("%s %ld bytes\n"), szfilepath, filesize.QuadPart);
}
} while (FindNextFile(hFind, &ffd) != 0);
if (isEmpty)
_tprintf(TEXT("%s [DIR]\n"), szDir);
FindClose(hFind);
return true;
}
Использование:
TCHAR dir[MAX_PATH] = _T("D:\\");
RetrieveFile(dir);