0

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
heash
  • 23
  • 7
  • well, what is searchword, did you check that you are trying to find what you actually mean to? – Netwave May 05 '22 at 11:29
  • Yes, till " if e_name.contains(&searchword.replace" I made println and always got back the correct path and searchword – heash May 05 '22 at 11:34
  • can you post both examples? – Netwave May 05 '22 at 11:35
  • Going out on a limb here, but could this be the cause: https://stackoverflow.com/questions/27773313/why-does-my-string-not-match-when-reading-user-input-from-stdin? – Jmb May 05 '22 at 12:44
  • Added the printlns – heash May 09 '22 at 06:08
  • Looks like I was right about the duplicate. Note that I don't get the same output as you when I run your code: there should be empty lines each time you print `searchvalue` – Jmb May 09 '22 at 06:40
  • The duplicate applied to your situation: `let searchvalue = suche.trim().to_string();` – Jmb May 09 '22 at 06:42

0 Answers0