7 releases
0.3.1 | Apr 14, 2024 |
---|---|
0.3.0 | Apr 14, 2024 |
0.2.3 | Apr 14, 2024 |
0.2.0 | Mar 10, 2023 |
0.1.3 | May 31, 2022 |
#288 in Filesystem
176 downloads per month
Used in densky
43KB
816 lines
recv-dir
Simple recursive directory traversal.
Description
Traverse directories recursively and conditionally using iterators.
Example
Visit all directories
fn main() {
use recv_dir::RecursiveDirIterator;
let dir = RecursiveDirIterator::from_root("test_dir").unwrap();
for entry in dir {
println!("{:?}", entry);
}
}
Ignore symlinks
fn main() {
use std::path::Path;
use recv_dir::RecursiveDirIterator;
let dir = RecursiveDirIterator::with_closure_filter("test_dir", |dir: &Path| !dir.is_symlink()).unwrap();
for entry in dir {
println!("{:?}", entry);
}
}
Control depth
Visits only the files in test_dir
:
fn main() {
use std::path::{Path, PathBuf};
use recv_dir::RecursiveDirIterator;
let root = PathBuf::from("test_dir");
let ancestors = root.ancestors().count();
let dir = RecursiveDirIterator::with_closure_filter(root, |dir: &Path| dir.ancestors().count() - ancestors <= 1).unwrap();
for entry in dir {
println!("{:?}", entry);
}
}
Visits the files in test_dir
and in first-level subdirectories:
fn main() {
use std::path::{Path, PathBuf};
use recv_dir::RecursiveDirIterator;
let root = PathBuf::from("test_dir");
let ancestors = root.ancestors().count();
let dir = RecursiveDirIterator::with_closure_filter(root, |dir: &Path| dir.ancestors().count() - ancestors <= 2).unwrap();
for entry in dir {
println!("{:?}", entry);
}
}
You can also compose the filters:
fn main() {
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use recv_dir::{Filter, MaxDepth, NoSymlink, RecursiveDirIterator};
let root = PathBuf::from("test_dir");
let dir = RecursiveDirIterator::with_filter(
&root,
NoSymlink.and(MaxDepth::new(&root, NonZeroUsize::new(2).unwrap())),
).unwrap();
for entry in dir {
println!("{:?}", entry);
}
}
Read the documentation for more information
Dependencies
~0–5.5MB
~19K SLoC