-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_searchv3.rs
118 lines (99 loc) · 3.76 KB
/
file_searchv3.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::fs::{self, File};
use std::io::{self, Write, BufWriter};
use std::path::Path;
fn main() {
// Prompt the user for the search string
let search_string = prompt_for_search_string();
// Prompt the user for the directories to scan
let directories = prompt_for_directories();
// Ask the user if they want to write the results to a file
let write_to_file = prompt_to_write_to_file();
// Collect the results
let mut results = Vec::new();
for directory in directories {
let dir_path = Path::new(&directory);
if dir_path.is_dir() {
println!("Searching in directory: {}", directory);
search_files(dir_path, &search_string, &mut results);
} else {
println!("'{}' is not a directory. Skipping...", directory);
}
}
// Output results
if write_to_file {
let file_name = format!("my_{}_results.txt", search_string);
let file = File::create(&file_name).expect("Could not create file");
let mut writer = BufWriter::new(file);
for result in results {
writeln!(writer, "{}", result).expect("Could not write to file");
}
println!("Results written to {}", file_name);
} else {
for result in results {
println!("{}", result);
}
}
}
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 prompt_for_directories() -> Vec<String> {
let mut directories = Vec::new();
loop {
print!("Enter a directory to scan or 'done' to start scanning: ");
io::stdout().flush().unwrap();
let mut directory = String::new();
io::stdin().read_line(&mut directory).unwrap();
let directory = directory.trim().to_string();
if directory.to_lowercase() == "done" {
break;
}
directories.push(directory);
}
directories
}
fn prompt_to_write_to_file() -> bool {
loop {
print!("Do you want to write results to a file? (y/n): ");
io::stdout().flush().unwrap();
let mut answer = String::new();
io::stdin().read_line(&mut answer).unwrap();
let answer = answer.trim().to_lowercase();
match answer.as_str() {
"y" => return true,
"n" => return false,
_ => println!("Please enter 'y' or 'n'"),
}
}
}
fn search_files(dir: &Path, search_string: &str, results: &mut Vec<String>) {
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, results);
} else if let Some(file_name) = path.file_name() {
let file_name_str = file_name.to_string_lossy();
if search_string.starts_with("*.") {
// If the search string starts with "*.", search by file extension
let ext = &search_string[2..];
if path.extension().map_or(false, |e| e == ext) {
results.push(format!("Found: {:?}", path));
}
} else {
// Otherwise, search by substring
if file_name_str.contains(search_string) {
results.push(format!("Found: {:?}", path));
}
}
}
}
}
}
}