Listing the Files in a Directory - Win32 apps | Microsoft Docs[
^]
You have to check the Attribute and append new directory name in
ffd.cFileName
to path and call FindFirst again.
typedef std::basic_string<TCHAR> tstring;
using namespace std;
void printfiles(tstring inputstr)
{
TCHAR szDir[MAX_PATH];
tstring seachstr = inputstr + TEXT("\\*");
wcscpy_s(szDir, MAX_PATH, seachstr.c_str());
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind) {
_tprintf(TEXT("Error FindFirstFile\n"));
return;
}
do {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
if (!_tcscmp(ffd.cFileName, TEXT(".")) ||
!_tcscmp(ffd.cFileName, TEXT(".."))) continue;
tstring dirstr = inputstr + TEXT("\\") + ffd.cFileName;
_tprintf(TEXT(" %s <DIR>\n"), dirstr.c_str());
printfiles(dirstr);
}
else {
LARGE_INTEGER filesize;
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
_tprintf(TEXT(" %s %lld bytes\n"),
ffd.cFileName, filesize.QuadPart);
}
} while (FindNextFile(hFind, &ffd) != 0);
DWORD dwError;
dwError = GetLastError();
if (dwError != ERROR_NO_MORE_FILES) {
_tprintf(TEXT("Error while FindFile\n"));
}
FindClose(hFind);
}