如何在C ++中列出特定扩展名的文件夹中的文件名?

时间:2015-02-28 07:09:19

标签: c++ file directory

对于ex,我想要从c:/ user / documents文件夹中删除所有.pdf文件

1 个答案:

答案 0 :(得分:0)

重复How to get list of files with a specific extension in a given folder

#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;

    }

}