Search code examples
rustdirectoryglob

Working with user directories with directories and glob in rust


I am trying to learn rust, and my current objective is to be able to locate .js files in the user's documents directory under a subfolder.

I can currently list all js files in said subfolder.

    if let Some(user_dir) = UserDirs::new() {
        let script_path = user_dir.document_dir().unwrap().join("test").join("**").join("*.js");
        dbg!(&script_path);
        for script in glob(script_path.to_str().unwrap()).unwrap(){
            dbg!(script);
        }
    }

This would do nothing in the case where the folder test didnt exist. How would I go on about handling this case and furthermore create the folder test?


Solution

  • This would do nothing in the case where the folder test didnt exist. How would I go on about handling this case

    Store the intermediate path and call Path::exists.

    furthermore create the folder test?

    fs::create_dir_all.