Count the files in a folder

cplusplus forum,

windows 7
MS VS 2017 C++

Is there some code I can write to get a count of the number of files
in a folder?

Jerryd


Use the <filesystem> library in C++17. Use directory_iterator.
https://en.cppreference.com/w/cpp/filesystem/directory_iterator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <filesystem>

int main()
{
   int count { };

   std::filesystem::path p1 { "C:\\" };

   for (auto& p : std::filesystem::directory_iterator(p1))
   {
      ++count;
   }

   std::cout << "# of files in " << p1 << ": " << count << '\n';
}
Hello otherwise you can do everything with <windows.h> as I started to do, instead of reading the file or the file title delimit a variable for the number of files and add one to each new file. This is what I started to search for files but it will be easy to adapt it to what you want to do: http://www.cplusplus.com/forum/beginner/272346/
Topic archived. No new replies allowed.