Directory Iterators

Stop Directory Iteration Early

How can I stop the iteration prematurely when using directory_iterator?

Abstract art representing computer programming

You can stop the iteration prematurely by using the break statement within your loop. This allows you to exit the loop based on a specific condition or requirement.

Here’s an example where we stop the iteration after finding a specific file:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

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

  for (auto iter{start}; iter != end; ++iter) {
    std::cout << iter->path().string() << '\n';
    if (iter->path().filename() == "stop_here.txt") {
      break; // Stop iteration  
    }
  }
}
c:\test\file1.txt
c:\test\file2.txt
c:\test\stop_here.txt

Common Use Cases for Stopping Early:

  • Finding a Specific File or Directory: Stop once a specific entry is found.
  • Resource Limits: Stop if a certain resource limit is reached (e.g., memory usage, number of processed files).
  • User Input: Allow the user to interrupt the process based on input or an external event.

Example: Stopping Based on File Count:

Here’s another example where we stop after processing a certain number of files:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

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

  int count = 0;
  // Maximum files to process
  const int max_files = 5;  

  for (auto iter{start}; iter != end; ++iter) {
    if (count >= max_files) {  
      break; // Stop after processing max_files  
    }
    std::cout << iter->path().string() << '\n';
    ++count;
  }
}
c:\test\file1.txt
c:\test\file2.txt
c:\test\file3.txt
c:\test\file4.txt
c:\test\file5.txt

Handling Complex Conditions:

For more complex conditions, you can combine multiple checks:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

bool should_stop(const fs::directory_entry& entry) {
  // Example condition: stop at specific file
  // or if file size exceeds limit
  return entry.path().filename() == "stop_here.txt"
    || entry.is_regular_file()
    && entry.file_size() > 1024;  
}

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

  for (auto iter{start}; iter != end; ++iter) {
    if (should_stop(*iter)) {
      break;  
    }
    std::cout << iter->path().string() << '\n';
  }
}
c:\test\file1.txt
c:\test\file2.txt
c:\test\file3.txt

Stopping the iteration prematurely is a straightforward process in C++. Use conditions and the break statement to control the flow of your directory traversal as needed.

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:

  • 124 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