Skip to content

Instantly share code, notes, and snippets.

@marc0x71
Created July 2, 2022 15:36
Show Gist options
  • Save marc0x71/ed899b15f70e67e3e6f099d07ff48476 to your computer and use it in GitHub Desktop.
Save marc0x71/ed899b15f70e67e3e6f099d07ff48476 to your computer and use it in GitHub Desktop.
Recursive directory file finder
void filelist(const std::string& path, const std::string& match_str, std::function<void(std::string, std::string)> f) {
std::regex re(match_str);
auto it = std::filesystem::recursive_directory_iterator(path, std::filesystem::directory_options::skip_permission_denied);
auto it_end = std::filesystem::end(it);
while (it != it_end) {
try {
auto dir_entry = *it;
if (dir_entry.is_regular_file()) {
auto filename = dir_entry.path().filename().string();
auto folder = dir_entry.path().parent_path().string();
if (std::regex_match(filename, re))
f(folder, filename);
}
}
catch (std::filesystem::filesystem_error& e) {
std::cout << "Error! File skipped due to " << e.what() << "\n";
}
try {
++it;
}
catch (std::filesystem::filesystem_error& e) {
std::cout << "Error! " << e.what() << "\n";
}
}
std::cout << "filelist done!\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment