Directory Iterators

Skip Files or Directories using directory_iterator

How can I skip certain files or directories during iteration?

Abstract art representing computer programming

To skip certain files or directories during iteration, you can use conditional statements within your loop. By checking the properties of each entry, you can decide whether to process it or skip it.

Here’s an example where we skip files with a specific extension and directories with a specific name:

#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) {
    if (iter->is_directory() &&
      iter->path().filename() == "skip_this_dir") {
      continue;  // Skip this directory 
    }
    if (iter->is_regular_file() &&
      iter->path().extension() == ".skip") {
      continue;  // Skip files with .skip extension 
    }
    std::cout << iter->path().string() << '\n';
  }
}
c:\test\file1.txt
c:\test\file2.txt
c:\test\another_directory

Steps to Skip Entries:

  1. Check Directory Properties: Use is_directory() to check if the entry is a directory and path().filename() to get its name.
  2. Check File Properties: Use is_regular_file() to check if the entry is a file and path().extension() to get its extension.
  3. Skip Logic: Use continue to skip the current iteration if the conditions are met.

More Complex Filtering:

For more complex conditions, consider using a predicate function:

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

bool should_skip(const fs::directory_entry& entry) {
  if (entry.is_directory() &&
    entry.path().filename() == "skip_this_dir") {
    return true;
  }
  if (entry.is_regular_file() &&
    entry.path().extension() == ".skip") {
    return true;
  }
  return false;
}

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

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

This approach allows you to encapsulate the skip logic in a separate function, making your main loop cleaner and easier to read. Skipping certain files or directories during iteration is a common requirement and can be handled efficiently with these techniques.

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