C++ 如何获取给定文件夹中具有特定扩展名的文件列表?
我想获取给定文件夹(以及递归的子文件夹)中具有特定扩展名的所有文件的文件名。即文件名(和扩展名),而不是完整的文件路径。在Python这样的语言中,这是非常简单的,但是我不熟悉C++中的构造。如何做到这一点?你没有说你使用的是什么操作系统,但有几种选择 正如评论员所提到的,如果您可以使用boost,那么它将起作用 其他选择包括C++ 如何获取给定文件夹中具有特定扩展名的文件列表?,c++,filesystems,file-management,C++,Filesystems,File Management,我想获取给定文件夹(以及递归的子文件夹)中具有特定扩展名的所有文件的文件名。即文件名(和扩展名),而不是完整的文件路径。在Python这样的语言中,这是非常简单的,但是我不熟悉C++中的构造。如何做到这一点?你没有说你使用的是什么操作系统,但有几种选择 正如评论员所提到的,如果您可以使用boost,那么它将起作用 其他选择包括 在windows上,您可以执行以下操作: void listFiles( const char* path ) { struct _finddata_t d
void listFiles( const char* path )
{
struct _finddata_t dirFile;
long hFile;
if (( hFile = _findfirst( path, &dirFile )) != -1 )
{
do
{
if ( !strcmp( dirFile.name, "." )) continue;
if ( !strcmp( dirFile.name, ".." )) continue;
if ( gIgnoreHidden )
{
if ( dirFile.attrib & _A_HIDDEN ) continue;
if ( dirFile.name[0] == '.' ) continue;
}
// dirFile.name is the name of the file. Do whatever string comparison
// you want here. Something like:
if ( strstr( dirFile.name, ".txt" ))
printf( "found a .txt file: %s", dirFile.name );
} while ( _findnext( hFile, &dirFile ) == 0 );
_findclose( hFile );
}
}
在Posix上,如Linux或OsX:
void listFiles( const char* path )
{
DIR* dirFile = opendir( path );
if ( dirFile )
{
struct dirent* hFile;
errno = 0;
while (( hFile = readdir( dirFile )) != NULL )
{
if ( !strcmp( hFile->d_name, "." )) continue;
if ( !strcmp( hFile->d_name, ".." )) continue;
// in linux hidden files all start with '.'
if ( gIgnoreHidden && ( hFile->d_name[0] == '.' )) continue;
// dirFile.name is the name of the file. Do whatever string comparison
// you want here. Something like:
if ( strstr( hFile->d_name, ".txt" ))
printf( "found an .txt file: %s", hFile->d_name );
}
closedir( dirFile );
}
}
#定义BOOST_文件系统_版本3
#定义BOOST\u文件系统\u否\u已弃用
#包括
名称空间fs=boost::filesystem;
/**
*\brief返回具有指定扩展名的所有文件的文件名
*在指定的目录和所有子目录中。
*/
std::vector get_all(fs::path const和root,std::string const和ext)
{
std::向量路径;
if(fs::exists(root)&&fs::is_目录(root))
{
for(auto const&entry:fs::recursive_directory_iterator(root))
{
if(fs::is_regular_file(entry)&&entry.path().extension()=ext)
paths.emplace_back(entry.path().filename());
}
}
返回路径;
}
获取文件列表,处理每个文件,循环浏览并存储回不同的文件夹
void getFilesList(string filePath,string extension, vector<string> & returnFileName)
{
WIN32_FIND_DATA fileInfo;
HANDLE hFind;
string fullPath = filePath + extension;
hFind = FindFirstFile(fullPath.c_str(), &fileInfo);
if (hFind != INVALID_HANDLE_VALUE){
returnFileName.push_back(filePath+fileInfo.cFileName);
while (FindNextFile(hFind, &fileInfo) != 0){
returnFileName.push_back(filePath+fileInfo.cFileName);
}
}
}
void getFilesList(字符串文件路径、字符串扩展名、向量和返回文件名)
{
WIN32_查找_数据文件信息;
处理高频风;
字符串fullPath=文件路径+扩展名;
hFind=FindFirstFile(fullPath.c_str(),&fileInfo);
if(hFind!=无效的句柄值){
returnFileName.push_back(filePath+fileInfo.cFileName);
while(FindNextFile(hFind,&fileInfo)!=0){
returnFileName.push_back(filePath+fileInfo.cFileName);
}
}
}
使用:您可以像这样使用从文件夹加载所有文件并逐个循环
String optfileName ="";
String inputFolderPath ="";
String extension = "*.jpg*";
getFilesList(inputFolderPath,extension,filesPaths);
vector<string>::const_iterator it = filesPaths.begin();
while( it != filesPaths.end())
{
frame = imread(*it);//read file names
//doyourwork here ( frame );
sprintf(buf, "%s/Out/%d.jpg", optfileName.c_str(),it->c_str());
imwrite(buf,frame);
it++;
}
String optfileName=”“;
字符串inputFolderPath=“”;
字符串扩展名=“*.jpg*”;
GetFileList(inputFolderPath、扩展名、文件路径);
vector::const_迭代器it=filepath.begin();
while(it!=filepath.end())
{
frame=imread(*it);//读取文件名
//你在这里工作(框架);
sprintf(buf,“%s/Out/%d.jpg”,optfileName.c_str(),it->c_str());
imwrite(buf,帧);
it++;
}
一个C++17代码
#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
std::string path("/your/dir/");
std::string ext(".sample");
for (auto &p : fs::recursive_directory_iterator(path))
{
if (p.path().extension() == ext)
std::cout << p.path().stem().string() << '\n';
}
return 0;
}
#包括
#包括
#包括
名称空间fs=std::filesystem;
int main()
{
std::字符串路径(“/your/dir/”);
std::字符串ext(“.sample”);
for(auto&p:fs::recursive_directory_iterator(path))
{
if(p.path().extension()==ext)
std::cout这是我的解决方案(适用于*nix系统):
#包括
bool FindAllFiles(std::字符串路径、std::字符串类型、std::向量和文件列表){
DIR*DIR;
结构导向;
FileList.clear();
if((dir=opendir(path.c_str())!=NULL){
//检查此目录中的所有文件
while((ent=readdir(dir))!=NULL){
std::string filename=std::string(ent->d_name);
如果(filename.length()>4){
std::string ext=filename.substr(filename.size()-3);
if(ext==类型){
//如果此文件的类型正确,请存储它
FileList.push_back(文件名);
}
}
}
closedir(dir);
}否则{
//无法打开目录
STR::Cyr是很好的文件。在Python上编写C++必须感觉到在C++之后的汇编语言中的编写:)就标准C++而言,这是一个令人惊讶的代码密集型任务。我建议使用<代码> Boo::文件系统< /代码>。这是可移植的答案。唯一对我不起作用的是“和”。“,我将其替换为“&&”。其余的都很好。+1注意:对于扩展,不要忘记添加一个点…例如“.jpg”是正确的,而不是“jpg”。@Jav_Rock您可能正在使用VS,并且必须包含以获取非符号逻辑运算符。代码是使用名称空间std;
(应该添加)您的解决方案将在模式“Fo.txt.exe”中找到文件。我不认为这有一个.txt扩展。在Windows上,您在哪里设置“GigReNebug”的值。。它是一个标志吗?是的-在示例中是一个全局布尔值。但实际上,您可以在此处执行任何操作,例如将它作为另一个参数传递给listFiles。在从Windows 8生成的x64上(?)从现在开始,我相信hFile应该是一个intptr而不是long。当调用_findnext时,使用long将导致不好的结果。由于C++17已经过时2年了,我认为您可以从包含和命名空间中删除实验性)
#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
std::string path("/your/dir/");
std::string ext(".sample");
for (auto &p : fs::recursive_directory_iterator(path))
{
if (p.path().extension() == ext)
std::cout << p.path().stem().string() << '\n';
}
return 0;
}
#include <dirent.h>
bool FindAllFiles(std::string path, std::string type, std::vector<std::string> &FileList){
DIR *dir;
struct dirent *ent;
FileList.clear();
if ((dir = opendir (path.c_str())) != NULL) {
//Examine all files in this directory
while ((ent = readdir (dir)) != NULL) {
std::string filename = std::string(ent->d_name);
if(filename.length() > 4){
std::string ext = filename.substr(filename.size() - 3);
if(ext == type){
//store this file if it's correct type
FileList.push_back(filename);
}
}
}
closedir (dir);
} else {
//Couldn't open dir
std::cerr << "Could not open directory: " << path << "\n";
return false;
}
return true;
}