We already implemented a tool that works like ls
on Linux/MacOS, or dir
on Windows, but just as these tools, it doesn't print the file size for directories.
In order to get the size equivalent of a directory, we would have to descend down into it and sum up the size of all files that it contains.
In this recipe, we will implement a tool that does just that. The tool can be run on any folder and will summarize the accumulated size of all directory entries.
In this section, we will implement an app that iterates over a directory and lists the file size of each entry. This is simple for regular files, but if we are looking at a directory entry that itself is a directory, then we have to look into it and summarize the size of all the files it holds.
- First, we need to include all the necessary headers and declare that we use namespace
std
andfilesystem
.
#include <iostream> #include <sstream> #include <iomanip> ...