特定のフォルダー (および再帰的にそのサブフォルダー) 内の特定の拡張子を持つすべてのファイルのファイル名を取得したいと考えています。つまり、完全なファイル パスではなく、ファイル名 (および拡張子) です。これは、Python などの言語では信じられないほど単純ですが、C++ での構成についてはよく知りません。どうすればそれができますか?
93519 次
6 に答える
74
#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
/**
* \brief Return the filenames of all files that have the specified extension
* in the specified directory and all subdirectories.
*/
std::vector<fs::path> get_all(fs::path const & root, std::string const & ext)
{
std::vector<fs::path> paths;
if (fs::exists(root) && fs::is_directory(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());
}
}
return paths;
}
于 2012-06-21T16:24:19.740 に答える
41
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;
}
于 2017-12-26T07:45:20.493 に答える
18
Windowsでは、次のようにします。
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 );
}
}
LinuxやOsXなどのPosixの場合:
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 );
}
}
于 2012-06-21T16:11:35.850 に答える
5
ファイルのリストを取得し、各ファイルを処理し、それらをループして別のフォルダーに保存します
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);
}
}
}
USE:このように使用して、フォルダーからすべてのファイルをロードし、1つずつループします
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++;
}
于 2014-05-06T23:34:43.707 に答える
2
于 2012-06-21T14:42:15.790 に答える
0
これが私の解決策です(* nixシステムで動作します):
#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;
}
明らかに、目的の拡張子を好きなものに変更してください。また、3文字型を想定しています。
于 2020-11-13T20:51:36.583 に答える