Browse Source

Finding files in PATH

master
Julio Biason 1 year ago
parent
commit
8765caacfd
  1. 34
      find_exe.rs

34
find_exe.rs

@ -0,0 +1,34 @@
use std::env;
use std::path::Path;
use std::path::PathBuf;
fn main() {
let args = env::args();
if args.len() != 2 {
println!("Missing executable name");
return;
}
let command = PathBuf::from(&args.last().unwrap());
println!("Searching for {:?}...", command);
if command.exists() {
println!("Command is here!");
return;
}
println!("Exe is in {:?}", find_in_path(&command));
}
fn find_in_path(command: &Path) -> Option<PathBuf> {
env::split_paths(&env::var_os("PATH")?)
.filter_map(|dir| {
let full_path = dir.join(&command);
if full_path.exists() {
Some(full_path)
} else {
None
}
})
.next()
}
Loading…
Cancel
Save