recently I try to learn Rust and because of this I try to make a Script which finds a specific String in a network path.
For example: \sharedfolder\2022-Tournament\Soccer\press...
The User can search for a String in the bolded Foldernames. Part of my Script:
let path = format!(r"\\sharedfolder\");
let path1 = Path::new(&path).to_string_lossy();
let strg_path = path1.to_string();
let entries = WalkDir::new(strg_path).min_depth(1).max_depth(3);
for entry in entries {
let entry = entry.unwrap();
let e_name = String::from(entry.path().to_string_lossy());
if e_name.contains(&searchword.replace(&['(', ')', ',', '\"', '.', ';', ':', '\''][..], "")) {
println!("[=] Found {} in {} !", &searchword, &e_name);
}
}
My Problem is that I can't find the string (searchword) in the path (e_name). What am I missing? .find or match also didn't work here.
Update 2022/05/09 As suggested by Netwave here printlns for the code:
use std::io;
use std::path::Path;
use std::process::Command;
use walkdir::WalkDir;
fn main() {
let mut suche = String::new();
println!("[?] What year are you searching?: ");
io::stdin()
.read_line(&mut suche)
.expect("[-] Fehlerhafte Eingabe!");
let searchvalue = suche.to_string();
println!("[*] DEBUG searchvalue: {}", searchvalue);
let path = format!(r"E:\Soccer-Tournament");
let path1 = Path::new(&path).to_string_lossy();
let strg_path = path1.to_string();
println!("[*] DEBUG strg_path: {}", strg_path);
let entries = WalkDir::new(strg_path).min_depth(0).max_depth(5);
for entry in entries {
let entry = entry.unwrap();
let e_name = String::from(entry.path().to_string_lossy());
println!("[*] DEBUG e_name: {}", e_name);
println!("[*] DEBUG searchvalue: {}", searchvalue);
if e_name.contains(&searchvalue.replace(&['(', ')', ',', '\"', '.', ';', ':', '\''][..], "")) {
println!("[=] Found {} in {} !", &searchvalue, &e_name);
}
}
}
fn _explorer_open(folder: String) {
println!( "Opening" );
Command::new( "explorer" )
.arg( folder )
.spawn( )
.unwrap( );
}
The result:
[?] What year are you searching?:
2021
[*] DEBUG searchvalue: 2021
[*] DEBUG strg_path: E:\Soccer-Tournament
[*] DEBUG e_name: E:\Soccer-Tournament
[*] DEBUG searchvalue: 2021
[*] DEBUG e_name: E:\Soccer-Tournament\2021
[*] DEBUG searchvalue: 2021
[*] DEBUG e_name: E:\Soccer-Tournament\2021\Data.txt
[*] DEBUG searchvalue: 2021
[*] DEBUG e_name: E:\Soccer-Tournament\2021\Bilder
[*] DEBUG searchvalue: 2021
[*] DEBUG e_name: E:\Soccer-Tournament\2021\Tree.txt
[*] DEBUG searchvalue: 2021
[*] DEBUG e_name: E:\Soccer-Tournament\2022
[*] DEBUG searchvalue: 2021
[*] DEBUG e_name: E:\Soccer-Tournament\2022\Bilder
[*] DEBUG searchvalue: 2021
[*] DEBUG e_name: E:\Soccer-Tournament\2022\Tree.txt
[*] DEBUG searchvalue: 2021
[*] DEBUG e_name: E:\Soccer-Tournament\2022\Data.txt
[*] DEBUG searchvalue: 2021
[*] DEBUG e_name: E:\Soccer-Tournament\2019
[*] DEBUG searchvalue: 2021
[*] DEBUG e_name: E:\Soccer-Tournament\2019\Data.txt
[*] DEBUG searchvalue: 2021
[*] DEBUG e_name: E:\Soccer-Tournament\2019\Bilder
[*] DEBUG searchvalue: 2021
[*] DEBUG e_name: E:\Soccer-Tournament\2019\Tree.txt
[*] DEBUG searchvalue: 2021