Getting all files of a specific type, in a specified folder using Boost. (returned in alphabetical order)
//Accepts a path to a directory
//a file extension
//and a list
//Puts all files matching that extension in the directory into the given list.
//Sorts and returns the results
void GetFilesOfTypeInDirectory(const boost::filesystem::path &directoryPath, const string &fileExtension, std::vector<boost::filesystem::path> &list)
{
if(!boost::filesystem::exists(directoryPath) || !boost::filesystem::is_directory(directoryPath))
{
std::cerr << directoryPath << "is not a directory." << std::endl;
return;
}
boost::filesystem::recursive_directory_iterator it(directoryPath);
boost::filesystem::recursive_directory_iterator endit;
while(it != endit)
{
if(boost::filesystem::is_regular_file(*it) && it->path().extension() == fileExtension) list.push_back(it->path());
++it;
}
//Sort the list using our path comparing function
std::sort(list.begin(), list.end(), PathSort);
}
bool PathSort(const boost::filesystem::path &first, const boost::filesystem::path &second)
{
return first.filename().string() < second.filename().string();
}