Home

Read Files In A Directory Recursively In Rust

rust
//! ```cargo
//! [package]
//! edition = "2021"
//! [dependencies]
//! walkdir = "2"
//! ```

use walkdir::{DirEntry, WalkDir};

fn is_hidden(entry: &DirEntry) -> bool {
    entry.file_name()
         .to_str()
         .map(|s| s.starts_with("."))
         .unwrap_or(false)
}

fn main() {
  let walker = WalkDir::new("recursive_test").into_iter();
  for entry in walker.filter_entry(|e| !is_hidden(e)) {
      let entry = entry.unwrap();
      println!("{}", entry.path().display());
  }
}
results start

- This version skips hidden files and directories

- It will error if it tries to access a file or directory it doesn't have permission to open

~ fin ~