使用FindFirstFileEx函数递归遍历目录

我调试了它,但我仍然不知道可能是什么罪魁祸首。

#ifndef UNICODE #define UNICODE #endif #include  #include  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; } 

输出:

F:\ $ RECYCLE.BIN
F:\ $ RECYCLE.BIN \ S-1-5-21-1271883188-2384997935-49719322-1000
F:\ $ RECYCLE.BIN \希捷
F:\ $ RECYCLE.BIN \ Seagate \ Seagate Dashboard 2.0
F:\ $ RECYCLE.BIN \ Seagate \ Seagate Dashboard 2.0 \系统卷信息
F:\ $ RECYCLE.BIN \ Seagate \ Seagate Dashboard 2.0 \ System Volume Information \ Video

“第一层”如何看起来如何:

在此处输入图像描述

你能指出我的错误吗?

内部应该是:

  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);