WIN32 C++ 遍历文件夹

本文深入探讨了Windows环境下文件遍历与查找的技术细节,通过使用C++实现了一个强大的文件遍历函数,能够处理目录及文件,支持路径、文件名及通配符输入。详细介绍了关键函数FindFirstFile和FindNextFile的使用方法,提供了完整的代码示例,旨在帮助开发者高效地在Windows系统中进行文件操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.youkuaiyun.com/lizhigang770/archive/2010/11/30/6045242.aspx

 

一、先介绍一个结构 WIN32_FIND_DATA

typedef struct _WIN32_FIND_DATA

{

   DWORD dwFileAttributes; // 文件属性
   FILETIME ftCreationTime; // 文件创建时间
   FILETIME ftLastAccessTime; // 文件最后一次访问时间
   FILETIME ftLastWriteTime; // 文件最后一次修改时间
   DWORD nFileSizeHigh; // 文件长度高 32 位
   DWORD nFileSizeLow; // 文件长度低 32 位
   DWORD dwReserved0; // 系统保留
   DWORD dwReserved1; // 系统保留
   TCHAR cFileName[ MAX_PATH ]; // 长文件名
   TCHAR cAlternateFileName[ 14 ]; // 文件的可选名

  } WIN32_FIND_DATA;

可以通过 FindFirstFile ()函数,根据文件路径把待操作文件的相关属性读取到 WIN32_FIND_DATA 结构中去:

四、下面是遍历代码

#include<windows.h>
#include<iostream>
#include<string>
using namespace std;

//只能处理目录:lpPath只能是路径
void find(char *lpPath)
{
    char szFind[MAX_PATH];
   char szFile[MAX_PATH];

    WIN32_FIND_DATA FindFileData;

    strcpy(szFind,lpPath);
    strcat(szFind,"//*.*");

    HANDLE hFind=::FindFirstFile(szFind,&FindFileData);
    if(INVALID_HANDLE_VALUE == hFind)    return;
    while(TRUE)
    {
        if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            if(FindFileData.cFileName[0]!='.')
            {
                strcpy(szFile,lpPath);
                strcat(szFile,"//");
                strcat(szFile,FindFileData.cFileName);
                find(szFile);
            }
        }
        else
        {     
               cout<<FindFileData.cFileName<<endl;
        }
        if(!FindNextFile(hFind,&FindFileData))
            break;
    }
    FindClose(hFind);
}
//可同时处理目录和文件:path可以是路径,也可以是文件名,或者文件通配符
void _find(string path)
{
     //取路径名最后一个"//"之前的部分,包括"//"
    string prefix=path.substr(0,path.find_last_of('//')+1);

    WIN32_FIND_DATA FindFileData;
    HANDLE hFind=::FindFirstFile(path.c_str(),&FindFileData);
    if(INVALID_HANDLE_VALUE == hFind)
    {
       cout<<"文件通配符错误"<<endl;
      return;
   }
    while(TRUE)
    {
      //目录
        if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            //不是当前目录,也不是父目录
            if(FindFileData.cFileName[0]!='.')
            {
                //查找下一级目录
                _find(prefix+FindFileData.cFileName+'//'+"*.*");
            }
        }
        //文件
        else
        {  
             cout<<FindFileData.cFileName<<endl;
        }
        if(!FindNextFile(hFind,&FindFileData))
              break;
    }
    FindClose(hFind);
}

void main()
{
     find("E:");//目录:E盘
     _find("E://*.*");//E盘下所有文件

    string str=".//";
      string path;
      cout<<"请输入文件通配符:"<<flush;
      cin>>path;
      str=str+path;
      find((char*)str.c_str());//可以处理".",".." 不可以处理"*","..//*"
      _find(str);//可以处理"*","..//*" 不可以处理".",".."
}

WIN32_FIND_DATA ffd;

HANDLE hFind = FindFirstFile("c://test.dat",&ffd);

二、函数 FindFirstFile

      1 、声明 : HANDLE FindFirstFile(LPCTSTR lpFileName,LPWIN32_FIND_DATA lpFindFileData);

2 、参数 :

lpFileName String ,欲搜索的文件名。可包含通配符,并可包含一个路径或相对路径名
lpFindFileData WIN32_FIND_DATA ,一个 WIN32_FIND_DATA 指针, 用于装载与找到的文件有关的信息,该结构可用于后续的搜索

3 、返回值 :

如执行成功,返回一个搜索句柄。如果出错,返回一个 INVALID_HANDLE_VALUE 常数,一旦不再需要,应该用 FindClose 函数关闭这个句柄

4 、功能

读取文件信息到一个 WIN32_FIND_DATA 结构中,返回这个文件的句柄

5 、注释:

由这个函数返回的句柄可以作为一个参数用于 FindNextFile 函数。这样一来,就可以方便的枚举出与 lpFileName 参数指定的文件名相符的所有文件(是指文件名含有通配符)

三、函数 FindNextFile

1 、声明 BOOL FindNextFile(HANDLE hFindFile, LPWIN32_FIND_DATA lpFindFileData);

2 、参数 :

HANDLE hFindFile 搜索的文件句柄,函数执行的时候搜索的是此句柄的下一文件

LPWIN32_FIND_DATA lpFindFileData 一个 WIN32_FIND_DATA 指针,

3 、返回值 :

如果调用成功返回一个非 0 值;调用失败,返回为 0 ,可调用 GetLastError 来获取错误信息

4 、功能

继续查找 FindFirstFile 函数搜索后的文件

5 、注释:

这个函数可以连续调用,以方便的枚举出与 lpFileName 参数指定的文件名相符的所有文件

 

其它参考文献:http://www.cnblogs.com/summerRQ/articles/2375749.html

### C++ 遍历文件夹的示例代码 以下是几种常见的方法来实现 C++ 文件夹遍历功能: #### 方法一:使用 `FindFirstFile` 和 `FindNextFile`(Windows 平台) 这是 Windows 提供的标准 API,适合用来查找和遍历文件夹中的文件。 ```cpp #include <windows.h> #include <tchar.h> #include <iostream> void traverseDirectory(const TCHAR* path) { WIN32_FIND_DATA findData; HANDLE hFind; _tcscpy_s(findData.cFileName, MAX_PATH, path); _tcscat_s(findData.cFileName, TEXT("\\*")); hFind = FindFirstFile(findData.cFileName, &findData); if (hFind == INVALID_HANDLE_VALUE) { std::cerr << "Error opening directory." << std::endl; return; } do { if (_tcscmp(findData.cFileName, TEXT(".")) && _tcscmp(findData.cFileName, TEXT(".."))) { if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // 如果是子目录,则递归遍历 TCHAR subPath[MAX_PATH]; _sntprintf_s(subPath, MAX_PATH, TEXT("%s\\%s"), path, findData.cFileName); traverseDirectory(subPath); } else { // 输出文件路径 std::wcout << path << "\\" << findData.cFileName << std::endl; } } } while (FindNextFile(hFind, &findData)); FindClose(hFind); } int main() { const TCHAR* targetDir = TEXT("C:\\YourTargetDirectory"); traverseDirectory(targetDir); return 0; } ``` 这种方法利用了 Windows 的 `FindFirstFile` 和 `FindNextFile` 函数[^3],能够高效地遍历目标文件夹及其子文件夹中的所有文件。 --- #### 方法二:使用 POSIX 接口(跨平台) 如果需要支持 Linux 或其他类 Unix 系统,可以采用标准库 `<dirent.h>` 进行文件夹遍历。 ```cpp #include <dirent.h> #include <stdio.h> #include <string.h> void processFiles(const char* folderPath) { DIR* dir = opendir(folderPath); if (!dir) { fprintf(stderr, "Cannot open directory: %s\n", folderPath); return; } struct dirent* entry; while ((entry = readdir(dir))) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; char fullPath[1024]; snprintf(fullPath, sizeof(fullPath), "%s/%s", folderPath, entry->d_name); if (entry->d_type == DT_DIR) { // 如果是子目录,则递归遍历 processFiles(fullPath); } else { // 处理文件逻辑 printf("Found file: %s\n", fullPath); } } closedir(dir); } int main() { const char* dataFolder = "/path/to/your/folder"; processFiles(dataFolder); return 0; } ``` 此代码片段展示了如何通过 POSIX 接口访问文件系统,并递归遍历整个目录结构[^2]。 --- #### 方法三:使用 OpenCV 的 `glob()` 函数 OpenCV 库提供了一个简单的工具函数 `cv::glob()`,可以直接用于检索指定模式的文件列表。 ```cpp #include <opencv2/core.hpp> #include <iostream> void listFilesWithGlob(const std::string& pattern) { std::vector<std::string> files; cv::glob(pattern, files, false); for (const auto& file : files) { std::cout << file << std::endl; } } int main() { std::string searchPattern = "C:/YourTargetDirectory/*.jpg"; // 修改为你想要的路径和扩展名 listFilesWithGlob(searchPattern); return 0; } ``` 该方法特别适用于图像处理场景,因为它允许轻松过滤具有特定后缀的文件[^1]。 --- #### 方法四:借助第三方库 Boost.Filesystem Boost 是一个强大的 C++ 扩展库集合,其中 `filesystem` 模块提供了丰富的接口来进行文件操作。 ```cpp #include <boost/filesystem.hpp> #include <iostream> namespace fs = boost::filesystem; void traverseDirectory(fs::path root) { if (!fs::exists(root) || !fs::is_directory(root)) return; for (auto& entry : fs::recursive_directory_iterator(root)) { if (fs::is_regular_file(entry.path())) { std::cout << entry.path().string() << std::endl; } } } int main() { fs::path targetDir("C:/YourTargetDirectory"); traverseDirectory(targetDir); return 0; } ``` 这种方式不仅简洁明了,而且兼容多种操作系统环境[^3]。 --- ### 总结 以上四种方式分别针对不同需求进行了优化设计。具体选择取决于项目运行的目标平台以及开发者的个人偏好。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值