c++ - 你如何让 FindFirstFile 打开你给它的路径的子目录?

标签 c++

我想遍历目录的所有子目录以找到所有 .snt 文件。但是 FindFirstFile 和 FindNextFile 只搜索给定的目录,而不是它的子目录。

具体来说,我正在搜索路径 F:\Program Files (x86)\Amnesia\sounds\
sounds 文件夹的所有子文件夹 我尝试将 F:\\Program Files (x86)\\Amnesia\\sounds\\*\\*.snt 传递给 FindFirstFile 但它返回垃圾。执行此操作的正确方法是什么?

#include <windows.h>
#include <stdio.h>
#include <iostream.h>
#include <conio.h>

void main()
{
    char  path[100]="F:\\Program Files (x86)\\Amnesia\\sounds\\*\\*.snt";
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    hFind = FindFirstFile(path, &FindFileData);
    printf("The first file found is %s\n",FindFileData.cFileName);
    getch();
}

最佳答案

好吧,“手动”递归。

下面是递归的思路。缺少的是基于扩展名的文件检测。

void file_search_rec(const char *folder)
{
    char path[MAX_PATH] ;
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;

    strcpy(path, folder) ;
    strcat(path, "\\*") ;
    FindFirstFile(path, &FindFileData) ;
    if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
        char subpath[MAX_PATH] ;
        strcpy(subpath, folder) ;
        strcat(subpath, FindFileData.cFileName) ;
        // here make the recursive call on subpath
        file_search_rec(subpath) ;
    }
}

关于c++ - 你如何让 FindFirstFile 打开你给它的路径的子目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31117830/

相关文章:

c++ - 使用迭代器时的性能问题?

c++ - 如何判断一个 lib 是用/mt 还是/md 编译的?

显示程序特定部分的调用路径的 C++ 分析器

c++ - 添加到 OpenGL 中的部分图像而不是完全重绘?

c++ - vim在现有或新标签页打开编译报错

c++ - 将基于 Netbeans 的已编译 C++ 程序作为 CGI 运行,给出权限错误

c++ - 如何定义指向函数指针的指针以及如何在 C++ 中使用它?

C++:结构的构造函数?

c++ - 为什么可变说明符被分类为存储类说明符,而不是限定符?

c++ - 我如何以编程方式找出 IIS 服务器有多少资源以及每个资源的流量..等等