How find the name of specific file in folder?

Hi, i have created this method and work fine, but i want to get and compare specific name. Ej. only "book.txt"
This code:

let books = "/home/pichi/books/";

// return true if .txt
fn check_txt(cadena: &str) -> bool {
    let txt_ext = vec![".txt", ".ls"];
    for ext in &txt_ext {
        if cadena.ends_with(ext) {
            return true;
        }
    }
    return false;
}
fn main() {
for entry in fs::read_dir(&books)? {
        let path = entry?.path();
        // Get path string.
        let path_str = path.to_str().unwrap(); // original name
        if path.is_dir() {            
            for entry2 in fs::read_dir(&path_str)? {
                let path2 = entry2?.path();
                // Get path string.
                let path2_str = path2.to_str().unwrap(); // root + name + extension
                if check_txt(&path2_str) != false {
                    println!("Files txt: {} ", &path2_str);
                }                

            }            
        }
    } // end entry

}

:face_with_peeking_eye:

DirEntry::file_name
Path::file_name

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.