//用C++实现获取某个文件下,包含特殊后缀名的所有文件名
#include <iostream>
#include <vector>
#include <string>
#include <boost/filesystem.hpp>
bool getFilesFromFolder(const std::string &folder,
const std::string &suffix,
std::set<std::string> &files)
{
boost::filesystem::path path(folder);
if (!boost::filesystem::exists(path) || !boost::filesystem::is_directory(path))
{
std::cout << "the folder is not exists or is not directory. the folder: " << folder << std::endl;
return false;
}
boost::filesystem::recursive_directory_iterator end_iter;
for(boost::filesystem::recursive_directory_iterator iter(path); iter != end_iter; ++iter)
{
//check file
if(boost::filesystem::is_regular_file(*iter) && iter->path().string().size() > suffix.size())
{
std::string curSuffix = iter->path().string().substr(iter->path().string().size() - suffix.size());
if (suffix == curSuffix)
{
files.insert(iter->path().string());
}
}
else if(boost::filesystem::is_directory(iter->status()))
{
std::string dirNext(iter->path().string());
getFilesFromFolder(dirNext, suffix, files);
}
}
return true;
}