且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获得给定文件夹中具有特定扩展名的文件的列表?

更新时间:2022-11-28 11:38:59

  #define BOOST_FILESYSTEM_VERSION 3 
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include< boost / filesystem.hpp>

namespace fs = :: boost :: filesystem;

//返回具有指定扩展名的所有文件的文件名
//在指定的目录和所有子目录中
void get_all(const fs :: path& root,const (&);< fs :: path_>& ret)


fs :: recursive_directory_iterator it(root);
fs :: recursive_directory_iterator endit;如果(fs :: is_regular_file(* it)& it-> path()。extension()=($!


) = ext)ret.push_back(it-> path()。filename());
++ it;



$ b code $ pre

I want to get the file names of all files that have a specific extension in a given folder (and recursively, its subfolders). That is, the file name (and extension), not the full file path. This is incredibly simple in languages like Python, but I'm not familiar with the constructs for this in C++. How can it be done?

#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED 
#include <boost/filesystem.hpp>

namespace fs = ::boost::filesystem;

// return the filenames of all files that have the specified extension
// in the specified directory and all subdirectories
void get_all(const fs::path& root, const string& ext, vector<fs::path>& ret)
{
    if(!fs::exists(root) || !fs::is_directory(root)) return;

    fs::recursive_directory_iterator it(root);
    fs::recursive_directory_iterator endit;

    while(it != endit)
    {
        if(fs::is_regular_file(*it) && it->path().extension() == ext) ret.push_back(it->path().filename());
        ++it;

    }

}