Created
July 2, 2022 15:36
-
-
Save marc0x71/ed899b15f70e67e3e6f099d07ff48476 to your computer and use it in GitHub Desktop.
Recursive directory file finder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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