Directory Iterators

Count Files in Directory

How can I count the number of files in a directory?

Abstract art representing computer programming

Counting the number of files in a directory is a common task that can be accomplished by iterating through the directory entries and incrementing a counter for each file.

Here’s an example that counts regular files in a directory:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::directory_iterator start{R"(c:\test)"};
  fs::directory_iterator end{};

  int file_count = 0;  // Initialize counter 

  for (auto iter{start}; iter != end; ++iter) {
    if (iter->is_regular_file()) {  
      ++file_count;
    }
  }

  std::cout << "Number of files: " << file_count;
}
Number of files: 3

Steps to Count Files:

  1. Initialize Iterator: Create a std::filesystem::directory_iterator for the target directory.
  2. Initialize Counter: Set a counter variable to zero.
  3. Iterate and Count: Loop through the directory entries, incrementing the counter for each regular file.

Counting Files and Directories Separately:

You can also count files and directories separately:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::directory_iterator start{R"(c:\test)"};
  fs::directory_iterator end{};

  int file_count = 0;
  int dir_count = 0;

  for (auto iter{start}; iter != end; ++iter) {
    if (iter->is_regular_file()) {
      ++file_count;
    } else if (iter->is_directory()) {
      ++dir_count;
    }
  }

  std::cout << "Number of files: "
    << file_count << '\n';
  std::cout << "Number of directories: "
    << dir_count << '\n';
}
Number of files: 3
Number of directories: 1

Using Range-Based Algorithms:

You can also use range-based algorithms to count files:

#include <filesystem>
#include <iostream>
#include <ranges>
namespace fs = std::filesystem;

int main() {
  auto entries = std::ranges::subrange{
    fs::directory_iterator{R"(c:\test)"},
    fs::directory_iterator{}
  };

  auto file_count = std::ranges::count_if(
    entries,
    [](const fs::directory_entry& entry) {
      return entry.is_regular_file();
    }
  );

  std::cout << "Number of files: " << file_count;
}
Number of files: 3

Counting with Recursive Iteration:

If you want to count files in a directory and its subdirectories, use std::filesystem::recursive_directory_iterator:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  fs::recursive_directory_iterator start{
    R"(c:\test)"};
  fs::recursive_directory_iterator end{};

  int file_count = 0;

  for (auto iter{start}; iter != end; ++iter) {
    if (iter->is_regular_file()) {
      ++file_count;
    }
  }

  std::cout << "Number of files: " << file_count;
}
Number of files: 3

Counting files is a fundamental operation that can be easily implemented using the techniques shown above, whether for flat or recursive directory structures.

This Question is from the Lesson:

Directory Iterators

An introduction to iterating through the file system, using directory iterators and recursive directory iterators

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Directory Iterators

An introduction to iterating through the file system, using directory iterators and recursive directory iterators

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved