11 inline std::error_code
TruncateFiles(
const std::filesystem::path& in_rPath, std::string_view in_extension, size_t in_max, T&& in_rrCompare)
13 std::error_code result{};
15 if (!std::filesystem::exists(in_rPath, result) || !std::filesystem::is_directory(in_rPath, result))
18 std::vector<std::filesystem::directory_entry> files{};
20 for (
const auto& entry : std::filesystem::directory_iterator(in_rPath))
22 if (!entry.is_regular_file(result))
25 if (entry.path().extension() != in_extension)
28 files.push_back(entry);
31 if (files.size() <= in_max)
34 std::sort(files.begin(), files.end(), in_rrCompare);
36 auto truncateLength = files.size() - in_max;
38 for (size_t i = 0; i < truncateLength; i++)
39 std::filesystem::remove(files[i].path(), result);
44 inline std::error_code
TruncateFilesByAge(
const std::filesystem::path& in_rPath, std::string_view in_extension, size_t in_max)
46 std::error_code result{};
49 return TruncateFiles(in_rPath, in_extension, in_max, [&](
const auto& a,
const auto& b)
51 return std::filesystem::last_write_time(a, result) < std::filesystem::last_write_time(b, result);