Directory Iterators

Iterate Multiple Directories

How do I iterate over multiple directories in one loop?

Abstract art representing computer programming

Iterating over multiple directories in one loop can be accomplished by initializing iterators for each directory and processing them in sequence.

You can use a container to store the directory paths and loop through each path with a std::filesystem::directory_iterator.

Here’s an example that iterates over multiple directories:

#include <filesystem>
#include <iostream>
#include <vector>

namespace fs = std::filesystem;

int main() {
  std::vector<fs::path> directories{
    R"(c:\test1)", R"(c:\test2)", R"(c:\test3)"};  

  for (const auto& dir : directories) {
    std::cout << "Directory: " << dir.string() << '\n';
    fs::directory_iterator start{dir};
    fs::directory_iterator end{};

    for (auto iter{start}; iter != end; ++iter) {
      std::cout << iter->path().string() << '\n';
    }
  }
}
Directory: c:\test1
c:\test1\file1.txt
c:\test1\file2.txt
Directory: c:\test2
c:\test2\fileA.txt
c:\test2\fileB.txt
Directory: c:\test3
c:\test3\fileX.txt
c:\test3\fileY.txt

Steps to Iterate Multiple Directories:

  1. List Directories: Create a container (e.g., std::vector<fs::path>) to store the directory paths.
  2. Loop Through Directories: Use a loop to iterate over each directory path.
  3. Initialize and Iterate: For each directory, initialize a std::filesystem::directory_iterator and process the entries.

Using a Recursive Approach:

If you need to handle nested directories, consider a recursive function:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

void process_directory(const fs::path& dir) {
  std::cout << "Directory: " << dir.string() << '\n';
  fs::directory_iterator start{dir};
  fs::directory_iterator end{};

  for (auto iter{start}; iter != end; ++iter) {
    if (iter->is_directory()) {
      // Recurse into subdirectory
      process_directory(iter->path());  
    } else {
      std::cout << iter->path().string() << '\n';
    }
  }
}

int main() {
  std::vector<fs::path> directories{
    R"(c:\test1)", R"(c:\test2)", R"(c:\test3)"};

  for (const auto& dir : directories) {
    process_directory(dir);  
  }
}
Directory: c:\test1
c:\test1\file1.txt
c:\test1\file2.txt
Directory: c:\test2
c:\test2\fileA.txt
c:\test2\fileB.txt
Directory: c:\test3
c:\test3\fileX.txt
c:\test3\fileY.txt

Using a Queue for Iteration:

For a non-recursive approach, use a queue to manage directories:

#include <filesystem>
#include <iostream>
#include <queue>

namespace fs = std::filesystem;

int main() {
  std::queue<fs::path> dirs;
  dirs.push(R"(c:\test1)");
  dirs.push(R"(c:\test2)");
  dirs.push(R"(c:\test3)");

  while (!dirs.empty()) {
    auto dir = dirs.front();
    dirs.pop();
    std::cout << "Directory: " << dir.string() << '\n';

    fs::directory_iterator start{dir};
    fs::directory_iterator end{};

    for (auto iter{start}; iter != end; ++iter) {
      std::cout << iter->path().string() << '\n';
      if (iter->is_directory()) {
        // Add subdirectory to queue
        dirs.push(iter->path());  
      }
    }
  }
}
Directory: c:\test1
c:\test1\file1.txt
c:\test1\file2.txt
Directory: c:\test2
c:\test2\fileA.txt
c:\test2\fileB.txt
Directory: c:\test3
c:\test3\fileX.txt
c:\test3\fileY.txt

These techniques allow you to iterate over multiple directories efficiently, processing their contents as needed. Whether using simple loops, recursion, or a queue, you can handle complex directory structures with ease.

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