Fs::read_dir and push in vector folder

Hi, i iterate over multiple folders, and push their names in a vector, except a folder where the name is "20.000 species of bee". it shows like this "20" in vector. why ?

Thanks

...
// .var store result
    let mut genre_vector: Vec<String> = Vec::new();
    
    // iterate over movies_src and store in genre_vector
    // a=genre, b=movie folder,    
    for a in fs::read_dir(movies_src.to_str().unwrap())? {
        let a = a?.path();
        genre_vector.push(a.file_stem().expect("REASON").to_str().unwrap().to_string());
    }
    genre_vector.sort();
    
    
    // .iter in genre_vector
    for gen in genre_vector.iter() {        
        let genre_path = Path::new(movies_src).join(gen);
        //println!("path {}", genre_path.display());
        // .movies folder
        let mut movies_vector: Vec<String> = Vec::new();       
        for movies in fs::read_dir(genre_path.to_str().unwrap())? {
            let movies = movies?.path();
            movies_vector.push(movies.file_stem().expect("REASON").to_str().unwrap().to_string());            
            // ? path to string vector Err in 20.000 species of b ...
            
            //println!("path {}", movies.display());            
        } // end for movies
        movies_vector.sort();
        println!("movies in vector {:?}", movies_vector);
        
        // .iter in movies_vector
        for movie in movies_vector.iter() {
            let movie_path = Path::new(movies_src).join(gen).join(movie);
            //println!("movie folder: {}", movie_path.display());
            
             for file in fs::read_dir(movie_path.to_str().unwrap())? {
                 let movie_file = file?.path();
                 println!("file: {}", movie_file.display());
             } // end for file 
        } // end for movie
    } // end for gen
...

Your code is incomplete and looks like it contains irrelevant sections, so it's difficult to reproduce the problem.

To improve your chances of getting a solution, you should provide a minimal reproducible example with a simple main() function and an explanation of exactly what directories are needed to reproduce the bug reliably.

Edit: However, it looks like the problem is related to your use of file_stem(). Here is the documentation for the function:

Extracts the stem (non-extension) portion of self.file_name.

The stem is:

  • None, if there is no file name;
  • The entire file name if there is no embedded .;
  • The entire file name if the file name begins with . and has no other .s within;
  • Otherwise, the portion of the file name before the final .

Since your directory name has a . in it, the function is returning the portion of the file name before the final .

2 Likes

:face_with_hand_over_mouth: yes (file_stem()) there is the problem. what i can do, any suggestion ?

What do you want to do, at the first place? Since you haven't used the obvious file_name, it seems that you don't want to just use the name?

2 Likes