I wanna check whether "notepad.exe" is running, using below code but no expected result appeared, how ot use this Command::new() ?
use std::process::Command;
fn main(){
let output = if cfg!(target_os = "windows") {
println!("win");
Command::new("tasklist")
.arg("/FI")
.arg("IMAGENAME")
.arg("eq")
.arg("notepad.exe")
.output()
.expect("failed to execute process")
} else {
// just ignore linux. i dont need this part,
};
let hello = output.stdout;
println!("{:?}", &hello);
let output_str = String::from_utf8_lossy(&hello);
println!("{:?}", output_str);
}
original code running in cmd in windows as below, I want to put it into rust,
tasklist /FI "IMAGENAME eq notepad.exe"
the quoted string is passed as one argument instead of tree, so the equivalent is:
Command::new("tasklist")
.arg("/FI")
.arg("IMAGENAME eq notepad.exe")
.output()
.expect("failed to execute process")
I revised as below but still no feedback,
use std::process::Command;
fn main(){
let output = Command::new("cmd")
.arg("tasklist /FI \"IMAGENAME eq notepad.exe\"")
.output()
.expect("failed to execute process");
let h = output.stdout;
println!("{:?}", h);
let output_str = String::from_utf8_lossy(&h);
println!("{:?}", output_str);
}
oh my, don't play quotes with cmd, it's not worth it. try this:
Command::new("cmd")
.arg("/S")
.arg("/c")
.arg("tasklist")
.arg("/FI")
.arg("IMAGENAME eq notepad.exe")
.output()
.expect("failed to execute process")
1 Like
highly appreciated, thanks..