1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include <iostream>
#include <boost/filesystem.hpp>
#include <boost/regex.hpp>
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
int main(int argc, char* argv[])
{
using namespace boost::filesystem;
std::vector< std::string > mes_bons_fichiers;
path p("C:\\temp\\test");
try
{
if(exists(p) && is_directory(p))
{
// On parcourt le répertoire s'il existe
for(directory_iterator it = directory_iterator(p); it != directory_iterator(); it++)
{
// Si c'est bien un fichier, on vérifie que son nom correspond
if(it -> status().type() == regular_file)
{
static const boost::regex file_name_format("fichier[0-9]+_[a-zA-Z]{3}\\.txt");
std::string filename = (it -> path()).filename().string();
// Si le nom est ok, on stocke le nom du fichier
if(regex_match(filename,file_name_format))
mes_bons_fichiers.push_back(filename);
}
}
}
}
catch (const filesystem_error& ex)
{
std::cout << ex.what() << std::endl;
}
// Affichage des bons fichiers récupérés
foreach(std::string & fichier, mes_bons_fichiers)
std::cout << fichier << std::endl;
return 0;
} |
Partager