如何使用 Win API 获取文件夹中的文件列表(带过滤器)
How to get a list of files (with filter) in folder with Win API
我在互联网上搜索了有关获取目录中的文件列表的信息,但大多数答案都需要提升库。所以我为那些不想添加任何第三方库的人提出这个问题。
那么如何获取目录中的文件列表呢?我们还可以决定要获取的文件的模式(像所有文件一样,仅txt文件,文件名中包含特定文本的文件,...另外决定是否在子文件夹中搜索。
编辑:我看到这个问题被降级了,也许是因为我没有具体说明这个问题是针对 Win 32 而不是一般C++(我最近才意识到,当我说C++时,没有必要意味着 Win API(。
无论如何,在这个问题上,我的意思是要求 Win API。当然,一般C++是受欢迎的(因为它也可以与Windows一起使用,更好的是跨平台(。
C++17 有一个 std::filesystem::d irectory_iterator,可以用作
#include <string>
#include <iostream>
#include <filesystem>
namespace fis = std::filesystem;
int main()
{
std::string dir_path = "path_to_dir";
for (auto & i : fis::directory_iterator(dir_path))
std::cout << i << std::endl;
}
此外,std::filesystem::recursive_directory_iterator 也可以迭代子目录。
可以使用dirent.h,它也适用于Windows:
DIR *dir;
struct dirent *s_dir;
if ((dir = opendir ("c:\programs\")) != NULL) {
/* print all the files and directories */
while ((s_dir = readdir (dir)) != NULL) {
printf ("%sn", s_dir->d_name);
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}
所以这是函数
void GetSubfolderInDirectory(std::vector<TCHAR*> *lstFileOut, const TCHAR* szPath)
{
HANDLE hFind;
WIN32_FIND_DATA findData;
TCHAR szCurrentDir[MAX_PATH];
_tcscpy_s(szCurrentDir, MAX_PATH, szPath);
TCHAR szPatternDir[MAX_PATH];
_tcscpy_s(szPatternDir, MAX_PATH, szCurrentDir);
_tcscat_s(szPatternDir, MAX_PATH, _T("/*"));
TCHAR* szFullPath;
bool bIsDirectory;
if ((hFind = FindFirstFile(szPatternDir, &findData)) == INVALID_HANDLE_VALUE)
{
return;
}
do {
bIsDirectory = (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (bIsDirectory)
{
if (findData.cFileName[0] == '.')
{
continue;
}
szFullPath = (_TCHAR*)malloc(_MAX_PATH * sizeof(_TCHAR));
_tcscpy_s(szFullPath, MAX_PATH, szCurrentDir);
_tcscat_s(szFullPath, MAX_PATH, _T("\"));
_tcscat_s(szFullPath, MAX_PATH, findData.cFileName);
lstFileOut->push_back(szFullPath);
}
} while (FindNextFile(hFind, &findData));
FindClose(hFind);
}
void GetFilesInDirectory(std::vector<TCHAR*> *lstFileOut, const TCHAR* szPath,
const TCHAR* szPattern, bool bSearchSub)
{
HANDLE hFind;
WIN32_FIND_DATA findData;
TCHAR szCurrentDir[MAX_PATH];
_tcscpy_s(szCurrentDir, MAX_PATH, szPath);
TCHAR szPatternDir[MAX_PATH];
_tcscpy_s(szPatternDir, MAX_PATH, szCurrentDir);
_tcscat_s(szPatternDir, MAX_PATH, _T("/"));
_tcscat_s(szPatternDir, szPattern);
if ((hFind = FindFirstFile(szPatternDir, &findData)) == INVALID_HANDLE_VALUE)
return; /* No files found */
TCHAR* szFullPath;
bool bIsDirectory;
do {
szFullPath = (_TCHAR*)malloc(_MAX_PATH * sizeof(_TCHAR));
_tcscpy_s(szFullPath, MAX_PATH, szCurrentDir);
_tcscat_s(szFullPath, MAX_PATH, _T("\"));
_tcscat_s(szFullPath, MAX_PATH, findData.cFileName);
bIsDirectory = (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (findData.cFileName[0] == '.')
{
free(szFullPath);
continue;
}
if (bIsDirectory)
{
continue;
}
else
{
lstFileOut->push_back(szFullPath);
}
} while (FindNextFile(hFind, &findData));
FindClose(hFind);
if (bSearchSub)
{
std::vector<TCHAR*> lstDir;
GetSubfolderInDirectory(&lstDir, szCurrentDir);
int nFolderCount = lstDir.size();
for (int i = 0; i < nFolderCount; i++)
{
GetFilesInDirectory(lstFileOut, lstDir[i], szPattern, bSearchSub);
}
}
}
这是如何使用
TCHAR szCrtPath[MAX_PATH];
TCHAR szCrtPattern[MAXBYTE];
_tcscpy_s(szCrtPattern, _T("*.*"));
_tcscpy_s(szCrtPath, _MAX_PATH, _T("D:\MyFolder"));
vector<TCHAR*> lstFile;
GetFilesInDirectory(&lstFile, szCrtPath, szCrtPattern, true);
关于模式的一些解释
*.* // Get all files
*.txt // Get txt file only
*sometext* // Get file with "sometext" in size file name
也许有更好的方法来获取目录中的所有子文件夹(函数GetSubfolderInDirectory(。
相关文章:
- 读取文件的最后一行并输入到链接列表时出错
- 如何从远程 SFTP 服务器获取 HH-MM-SS 时间戳格式的文件列表
- 给定一个源文件,有没有办法要求 gcc 返回仅直接包含的头文件的列表?
- 是否可以将图像列表保存到文件中?
- 如何在 Windows 上获取带有 c++ 且没有 <dirent.h> 的特定扩展名的所有文件的列表
- 使用通配符的跨平台文件列表
- 对"列表"的引用不明确,包括头文件
- 从文本文件读取并保存到列表
- 从文本文件读取,然后将项目存储到列表中
- 是否可以使用 std::文件系统获取根名称列表?
- 从 .so/.o 文件中获取 C/cpp 中的静态库列表
- 如何将地图<列表<string>、矢量<string>>输出到输出文件?
- 如何显示目录中的文件列表
- 如何读取文件的内容,如果文件中的单词重复,请将它们添加到列表中?
- 使用命令列表解析自定义文件
- 如何通过在逗号处拆分将文件读入 2D 列表
- 正确的生成文件依赖项列表 (C++)
- 从一个cpp中给出所使用的头文件和hpp文件的列表
- C++中的递归列表文件不会进入所有子目录
- 在Visual Studio 2013中使用/fa制作列表文件