[C++]枚举目录内文件

 1 #include <windows.h>
 2 #include <iostream>
 3 #include <fstream>
 4 
 5 using namespace std;
 6 
 7 DWORD ListDir(LPSTR szPath)
 8 {
 9     WIN32_FIND_DATA ListFile;
10     HANDLE hListFile;
11     char szFilePath[MAX_PATH];
12     ofstream MenuTxt("Menu.txt"); 
13     
14     lstrcpy(szFilePath, szPath);
15     lstrcat(szFilePath, "\\*");
16     hListFile = FindFirstFile(szFilePath, &ListFile);
17     char *DirArray[1024];
18     int i, j;
19     if(hListFile != INVALID_HANDLE_VALUE)
20     {
21         do
22         {
23             if(lstrcmp(ListFile.cFileName, "..") == 0 || lstrcmp(ListFile.cFileName, ".") == 0)
24                 continue;
25             
26             if(!(ListFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
27             {
28                 //cout << "----" << ListFile.cFileName << endl;
29                 MenuTxt << "----" << ListFile.cFileName << endl;
30             }
31             else
32             {
33                 i = 0;
34                 DirArray[i] = ListFile.cFileName;
35                 i++;
36             }
37         }while(FindNextFile(hListFile, &ListFile));
38         for(j = 0; j < i - 1; j++)
39             ListDir(DirArray[j]);
40     }
41     FindClose(hListFile);
42     MenuTxt.close();
43     return 0;
44 }
45 
46 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
47                     LPSTR lpCmdLine, int nCmdShow)
48 {
49     CHAR szCurPath[MAX_PATH];
50     GetCurrentDirectory(MAX_PATH, szCurPath);
51     ListDir(szCurPath);
52     MessageBox(NULL, "目录文件已创建!", "提示:", MB_OK | MB_ICONEXCLAMATION);
53     return 0;
54 }

 

posted @ 2020-07-15 13:18  hoy0a1d  阅读(518)  评论(0编辑  收藏  举报