Yukitteru
Yukitteru

Reputation: 37

How to get a list of all files with a specific extension?

I'm trying to get a list of all .key and .crt for further placement in the right folders, but for some reason the template does not work, I get the path to the source directory from the user input and then turn it into a string for pattern matching, but something went wrong. Help me figure out what I'm doing wrong

fn main() {
    let mut input: String = String::new();
    print!("Введите путь до директории с файлами для сборки: ");
    let _ = stdout().flush();
    stdin()
        .read_line(&mut input)
        .expect("Неправильный путь до директории");
    let p = Path::new(&input);
    read_file(p);
}

fn get_all(mut p: &Path) {
    let source_file_glob = p.to_str().unwrap();
    for entry in glob(format!("{}/{}", source_file_glob, "*.key").as_str())
        .unwrap()
        .chain(glob(format!("{}/{}", source_file_glob, "*.crt").as_str()).unwrap())
    {
        match entry {
            Ok(path) => println!("{}", path.display()),
            Err(e) => println!("{}", e),
        }
    }
}

Upvotes: 1

Views: 801

Answers (1)

Solomon Ucko
Solomon Ucko

Reputation: 6109

Apparently std::io::Stdin::read_line/std::io::BufRead::read_line includes the trailing newline. Try using str::trim_end_matches: input.trim_end_matches('\n') (Note that it returns a slice of the original string, and does not modify it.)

Upvotes: 1

Related Questions