The new standard expected for 2017 adds std::filesystem
. Using it, how can I count the number of files (including sub-directories) in a directory?
I know we can do:
std::size_t number_of_files_in_directory(std::filesystem::path path)
{
std::size_t number_of_files = 0u;
for (auto const & file : std::filesystem::directory_iterator(path))
{
++number_of_files;
}
return number_of_files;
}
But that seems overkill. Does a simpler and faster way exist?
I do not think that a way to easily get amount of files in directory exist, but you can simplify your code by using
std::distance
instead of handwritten loop:You can get number of only actual files or apply any other filter by using
count_if
instead: