Skip to content

Instantly share code, notes, and snippets.

@boyter
Created August 19, 2018 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boyter/95afeb5cd3943fc4ab53e2198dd9b3e1 to your computer and use it in GitHub Desktop.
Save boyter/95afeb5cd3943fc4ab53e2198dd9b3e1 to your computer and use it in GitHub Desktop.
Walk directory in Rust
extern crate walkdir;
use walkdir::WalkDir;
use std::fs::File;
use std::io::Read;
fn main() {
let nul = 0;
let mut bytes_count: i32;
for entry in WalkDir::new("./").into_iter().filter_map(|e| e.ok()) {
if entry.file_type().is_file() {
let mut file = File::open(entry.path().display().to_string()).unwrap();
bytes_count = 0;
let mut s: Vec<u8> = Vec::with_capacity(file.metadata().unwrap().len() as usize);
file.read_to_end(&mut s).unwrap();
for b in s.into_iter() {
if b == nul {
println!("{} bytes={} binary file", entry.path().display().to_string(), bytes_count);
break
}
bytes_count += 1;
}
println!("{} bytes={}", entry.path().display().to_string(), bytes_count)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment