问题
I've debugged it, but I still don't have any idea what could be the culprit.
#ifndef UNICODE
#define UNICODE
#endif
#include <stdio.h>
#include <Windows.h>
void EndWithBackslash(TCHAR* string)
{
if(string[wcslen(string)-1] != TEXT('\\')) wcscat(string,TEXT("\\"));
}
void Browse(const TCHAR* curdir)
{
HANDLE hFoundFile;
WIN32_FIND_DATA foundFileData;
TCHAR buffer[MAX_PATH];
wcscpy(buffer,curdir);
EndWithBackslash(buffer);
SetCurrentDirectory(buffer);
hFoundFile = FindFirstFileEx(TEXT("*"),FINDEX_INFO_LEVELS::FindExInfoBasic,&foundFileData ,FINDEX_SEARCH_OPS::FindExSearchLimitToDirectories ,NULL , NULL);
if(hFoundFile != INVALID_HANDLE_VALUE)
{
do
{
if ((foundFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(foundFileData.cFileName,TEXT(".")) && wcscmp(foundFileData.cFileName,TEXT("..")) )
{
EndWithBackslash(buffer);
wcscat(buffer,foundFileData.cFileName);
wprintf(TEXT("%s\n"),buffer);
Browse(buffer);
}
}
while(FindNextFile(hFoundFile,&foundFileData));
FindClose(hFoundFile);
}
}
int main(void)
{
Browse(TEXT("F:\\"));
system("Pause");
return 0;
}
Output:
F:\$RECYCLE.BIN
F:\$RECYCLE.BIN\S-1-5-21-1271883188-2384997935-49719322-1000
F:\$RECYCLE.BIN\Seagate
F:\$RECYCLE.BIN\Seagate\Seagate Dashboard 2.0
F:\$RECYCLE.BIN\Seagate\Seagate Dashboard 2.0\System Volume Information
F:\$RECYCLE.BIN\Seagate\Seagate Dashboard 2.0\System Volume Information\Video
How does "the first layer" realy looks:

Could you point out my mistake(s)?
回答1:
The inner part should be:
TCHAR pszItemPath[MAX_PATH];
wcscpy(pszItemPath, buffer);
// NOTE: Now when we took a copy of buffer, we don't touch it so that next iteration would have it good and untouched
EndWithBackslash(pszItemPath);
wcscat(pszItemPath, foundFileData.cFileName);
wprintf(TEXT("%s\n"), pszItemPath);
来源:https://stackoverflow.com/questions/12334043/traversing-directories-recursivelly-using-findfirstfileex-function