Dup Ver Goto 📝

Recursive

PT2/lang/rust/recipes/filesystem does not exist
To
45 lines, 127 words, 1351 chars Page 'Recursive' does not exist.

See this at rust-lang-nursery.

Recursively stat files

This gets the size and modification time, but could easily get other info. This is ugly. I'll enquire on a forum about the proper way to write this

use std::fs;
use std::path::Path;
use std::time;
use walkdir::WalkDir;

#[derive(Debug)]
struct FileInfo {
    name: String,
    size: u64,
    mtime: time::SystemTime,
}

fn main() {
    let mut files_info: Vec<FileInfo> = Vec::new();
    for entry in WalkDir::new(".")
        .follow_links(false)
        .into_iter()
        .filter_map(|e| e.ok()) {
            let path = entry.path();
            if let Ok(metadata) = entry.metadata() {
                if ! entry.path_is_symlink() {
                    if let Ok(mtime) = metadata.modified() {
                        let size = metadata.len();
                        if let Some(name) = path.as_os_str().to_str() {
                            files_info.push( FileInfo { 
                                name: name.to_string(), 
                                size,
                                mtime } )
                        }
                    }
                }
            }
        }

    for entry in files_info {
        println!("{:?}",entry);
    }
}