获取windows文件夹目录

获取windows文件夹目录

 1 #include <iostream>
 2 #include <cstring>        // for strcpy(), strcat()
 3 #include <io.h>
 4 
 5 using namespace std;
 6 
 7 void listFiles(const char * dir);
 8 
 9 int main()
10 {
11     char dir[200];
12     cout << "Enter a directory: ";
13     cin.getline(dir, 200);
14 
15     listFiles(dir);
16 
17     return 0;
18 }
19 
20 void listFiles(const char * dir)
21 {
22     char dirNew[200];
23     strcpy_s(dirNew, dir);
24     strcat_s(dirNew, "\\*.*");    // 在目录后面加上"\\*.*"进行第一次搜索
25 
26     intptr_t handle;
27     _finddata_t findData;
28 
29     handle = _findfirst(dirNew, &findData);
30     if (handle == -1)        // 检查是否成功
31         return;
32 
33     do
34     {
35         if (findData.attrib & _A_SUBDIR)
36         {
37             if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
38                 continue;
39 
40             cout << findData.name << "\t<dir>\n";
41 
42             // 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
43             strcpy_s(dirNew, dir);
44             strcat_s(dirNew, "\\");
45             strcat_s(dirNew, findData.name);
46 
47             listFiles(dirNew);
48         }
49         else
50             cout << findData.name << endl;
51     } while (_findnext(handle, &findData) == 0);
52 
53     _findclose(handle);    // 关闭搜索句柄
54 }

 

posted @ 2019-04-20 14:22  漫天飘雪  阅读(1074)  评论(0编辑  收藏  举报