Create a disk usage counter
This is a simple utility that totals the size of every file in a directory and its sub-directories. It runs on both POSIX/Unix and Windows file systems.
How to do it…
This recipe is a utility to report the size of every file in a directory and its sub-directories, along with a total. We'll re-use some of the functions we've used elsewhere in this chapter:
- We start with a few convenience aliases:
namespace fs = std::filesystem; using dit = fs::directory_iterator; using de = fs::directory_entry;
- We also use our
format
specialization forfs::path
objects:template<> struct std::formatter<fs::path>: std::formatter<std::string> { template<typename FormatContext> auto format(const fs::path& p, FormatContext& ctx) { return format_to(ctx.out(), "{}", p.string()); } };
...