[cpp, c++] std::filesystem search extension
namespace fs = std::filesystem;
Use fs::recursive_directory_iterator
fs::recursive_directory_iterator iterates all subdirectories recursively of a directory.
is_directory, is_regular_file, extension
is_directory - check if the file status or path corresponds to a directory.
fs::is_directory(?); fs::directory_entry::is_directory();
is_regular_file - check if the file status or path corresponds to a regular file.
fs::is_regular_file(?); fs::directory_entry::is_regular_file();
Use utxcpp
utx::printe
utx::print_all
Code cpp/c++
#include <utxcpp/core.hpp> #include <vector> #include <filesystem> namespace fs = std::filesystem; int main(int argc, char * argv[]) try { if (argc != 3) throw std::runtime_error{"prog <path> <search extension>"}; const fs::path path = argv[1]; const std::string ext = argv[2]; std::vector<fs::path> searched; for (const auto & entry: fs::recursive_directory_iterator{path}) { if ( ! entry.is_directory() && entry.is_regular_file() && entry.path().has_filename() && entry.path().extension() == ext ) { searched.push_back(entry.path()); } } utx::print_all(searched); } catch (std::exception & error) { utx::printe("[std::exception]\n", error.what()); return 1; }
Comments
Display comments as Linear | Threaded