-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_files.cpp
52 lines (45 loc) · 1.25 KB
/
find_files.cpp
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
47
48
49
50
51
52
#include <filesystem>
#include <functional>
#include <iostream>
namespace fs = std::filesystem;
std::vector<fs::path> findFiles(const fs::path& directory, std::function<bool(const fs::path&)> filter)
{
auto result = std::vector<fs::path>{};
if (fs::exists(directory))
{
for (const auto& entry:
fs::recursive_directory_iterator(directory, fs::directory_options::follow_directory_symlink))
{
if (fs::is_regular_file(entry) && filter(entry))
{
result.push_back(entry);
}
}
}
return result;
}
int main()
{
// Find files containing prefix 'file_'
{
auto results = findFiles(fs::current_path() / "test",
[](fs::path const& p)
{
auto filename = p.wstring();
return filename.find(L"file_") != std::wstring::npos;
});
for (const auto& path: results)
{
std::cout << path << std::endl;
}
}
// Find files with extension '.cpp'
{
auto results =
findFiles(fs::current_path() / "test", [](const fs::path& path) { return path.extension() == L".dat"; });
for (const auto& path: results)
{
std::cout << path << std::endl;
}
}
}