Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Read directory files in Windows

When I try to read files from a directory in Windows I get this error:

Err(Os { code: 123, kind: Uncategorized, message: "El nombre de archivo, el nombre de directorio o la sintaxis de la etiqueta del volumen no son correctos." })

I use this path "D:\3D\HDRI"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

And this is my code:

use std::{fs, io};
use std::path::Path;

fn main() -> io::Result<()> {
    let mut folder_path = String::new();

    println!("Folder path:");
    io::stdin()
        .read_line(&mut folder_path)
        .expect("Failed to read line");

    let path = Path::new(&mut folder_path);
    let files = fs::read_dir(path);

    println!("{:?}", files);

    Ok(())
}

>Solution :

Try

let path = Path::new(folder_path.trim());

The read_line method will put \r\n at the end of folder_path.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading