随笔 - 106  文章 - 0  评论 - 10  阅读 - 12万

c++ 枚举目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using namespace std;
#include <string.h>
#include <iostream>
#include <cstring>
#include <windows.h>
#include <string>
#include <vector>
 
using namespace std;
 
void listFiles(const char* dir, vector<string>& result);
 
int main()
{
 
    vector<string> vec;
 
    listFiles("D:\\alantop_dir\\*.*", vec);
 
    cout << "file sum number = " << vec.size() << endl;
 
    //DeleteFile
 
    //_rmdir()
 
    //    DeleteDirectory(sTempDir)
 
    //    RemoveDirectory(sTempDir)
 
    for (int i = 0; i < (int)vec.size(); i++) {
        cout << vec[i] << endl;
    }
 
 
    return 0;
}
 
void listFiles(const char* dir, vector<string>& result)
{
 
    HANDLE hFind;
    WIN32_FIND_DATA findData;
    LARGE_INTEGER size;
    hFind = FindFirstFile(dir, &findData);
    if (hFind == INVALID_HANDLE_VALUE)
    {
        cout << "Failed to find first file!\n";
        return;
    }
    do
    {
        // 忽略"."和".."两个结果
        if (strcmp(findData.cFileName, ".") == 0 || strcmp(findData.cFileName, "..") == 0)
            continue;
        if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)    // 是否是目录
        {
            //cout << findData.cFileName << "\t<dir>\n";
            char path[MAX_PATH] = "\0";
            memcpy(path, dir, strlen(dir) - 3);
 
            strcat(path, findData.cFileName);
            cout << path << "---[dir]" << endl;
            strcat(path, "\\*.*");
            listFiles((const char*)path, result);
 
        }
        else
        {
            size.LowPart = findData.nFileSizeLow;
            size.HighPart = findData.nFileSizeHigh;
            //cout << findData.cFileName << endl;
            string dirstring(dir, dir + strlen(dir)-3);
            string filename = findData.cFileName;
            result.push_back(dirstring + filename);
            //"\t" << size.QuadPart << " bytes\n";
        }
    } while (FindNextFile(hFind, &findData));
 
 
    FindClose(hFind);
 
}

  

posted on   alantop  阅读(139)  评论(3编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示