zoukankan      html  css  js  c++  java
  • vc 列举目录下所有文件

    // Project1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    //
    
    #include "pch.h"
    #include "framework.h"
    #include "Project1.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    
    
    
    // 唯一的应用程序对象
    
    //CWinApp theApp;
    
    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!
    ";
            return;
        }
        do
        {
            // 忽略"."和".."两个结果 
            if (strcmp(findData.cFileName, ".") == 0 || strcmp(findData.cFileName, "..") == 0)
                continue;
            if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)    // 是否是目录 
            {
                //cout << findData.cFileName << "	<dir>
    ";
                char path[MAX_PATH] = "";
                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);
                //"	" << size.QuadPart << " bytes
    ";
            }
        } while (FindNextFile(hFind, &findData));
    
    
        FindClose(hFind);
    
    }
    

      

  • 相关阅读:
    C#进阶之路——10.C# 接口
    C#进阶之路——9.C# 抽象类
    C#进阶之路——8.C# 继承
    C#进阶之路——7.ASP.NET常用控件
    C#进阶之路——6.C#字符与字符串
    C#进阶之路——5.C#数组与集合
    C#进阶之路——4.C#类属性和方法
    C#进阶之路——3.C#应用程序编译与执行
    mongodb
    Mycat配置文件详解
  • 原文地址:https://www.cnblogs.com/alantop/p/14204233.html
Copyright © 2011-2022 走看看