gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:11:51 25 4
gpt4 key购买 nike

我想遍历目录的所有子目录以找到所有 .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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com