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
std::vector<fs::path>
) to store the directory paths.std::filesystem::directory_iterator
and process the entries.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
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.
Answers to questions are automatically generated and may not have been reviewed.
An introduction to iterating through the file system, using directory iterators and recursive directory iterators