-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_search.rs
46 lines (40 loc) · 1.44 KB
/
file_search.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::fs;
use std::io::{self, Write};
use std::path::Path;
fn main() {
// Prompt the user for the search string
let search_string = prompt_for_search_string();
// Search for files recursively starting from the current directory
let current_dir = Path::new(".");
println!("Searching for files containing '{}'", search_string);
search_files(current_dir, &search_string);
// Suggest a file name upon completion
println!(
"Suggested file name: my_{}_results.txt",
search_string
);
}
fn prompt_for_search_string() -> String {
print!("Enter the search string: ");
io::stdout().flush().unwrap(); // flush the output to ensure the prompt appears before the user input
let mut search_string = String::new();
io::stdin().read_line(&mut search_string).unwrap();
search_string.trim().to_string()
}
fn search_files(dir: &Path, search_string: &str) {
if let Ok(entries) = fs::read_dir(dir) {
for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
if path.is_dir() {
// Recursive call
search_files(&path, search_string);
} else if let Some(file_name) = path.file_name() {
if file_name.to_string_lossy().contains(search_string) {
println!("Found: {:?}", path);
}
}
}
}
}
}