This commit is contained in:
2026-10-13 17:08:05 +02:00
commit 9ab550a1cb
5 changed files with 244 additions and 0 deletions
+217
View File
@@ -0,0 +1,217 @@
use std::{
env,
error::Error,
fs::{DirEntry, read_dir, remove_file},
io::{self, Write},
path::Path,
process::Command,
};
use strsim::levenshtein;
struct FileInfo {
file: DirEntry,
}
fn main() {
if let Err(e) = run() {
eprintln!("Error: {e}");
let mut source = e.source();
while let Some(err) = source {
eprintln!("Caused by: {err}");
source = err.source();
}
std::process::exit(1);
}
println!("Success");
}
fn run() -> Result<(), Box<dyn Error>> {
let folder_path = get_folder_path()?;
let files = get_filenames(folder_path)?;
let duplicates = get_duplicates(&files)?;
review_duplicates(&duplicates)?;
// let _ = print_duplicates(&duplicates);
Ok(())
}
fn review_duplicates(duplicates: &Vec<Vec<&FileInfo>>) -> Result<(), Box<dyn Error>> {
let mut total_freed: u64 = 0;
for (i, group) in duplicates.iter().enumerate() {
if group.len() < 2 {
continue;
}
let mut largest_idx = 0;
let mut largest_size = 0;
for (j, file) in group.iter().enumerate() {
let size = file.file.metadata()?.len();
if size > largest_size {
largest_idx = j;
largest_size = size;
}
}
println!(
"\n========== Duplicate groups {} / {} ==========",
i + 1,
duplicates.len()
);
for (j, file) in group.iter().enumerate() {
let size = file.file.metadata()?.len();
let marker = if j == largest_idx { "KEEP" } else { " " };
println!(
"[{}] {} {} {:.2} GB",
marker,
j + 1,
file.file.file_name().to_string_lossy(),
size as f64 / 1_000_000_000.0,
);
}
println!("\nActions:");
println!(" y - Keep the largest file and delete the others");
println!(" n - Skip this group");
println!(" q - Quit");
print!("Your choice: ");
io::stdout().flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
match input.trim().to_lowercase().as_str() {
"n" => println!("Skipped group."),
"q" => {
println!("Review stopped.");
break;
}
"y" => {
for (j, file) in group.iter().enumerate() {
if j == largest_idx {
continue;
}
let path = file.file.path();
let size = file.file.metadata()?.len();
remove_file(&path)?;
total_freed += size;
println!("Deleted: {:?}", path);
}
}
_ => println!("Invalid choice, skipping group."),
}
}
println!("\n========== Summary ==========");
println!("Total space freed: {:.2} GB", total_freed / 1_000_000_000);
Ok(())
}
fn get_duplicates(files: &Vec<FileInfo>) -> Result<Vec<Vec<&FileInfo>>, Box<dyn Error>> {
let mut duplicates = Vec::new();
let mut touched = vec![false; files.len()];
for i in 0..files.len() {
let f = &files[i];
if touched[i] || i == files.len() - 1 {
continue;
}
let mut new_list = vec![f];
for j in (i + 1)..files.len() {
let tmp_f = &files[j];
let tmp_name = tmp_f.file.file_name().to_string_lossy().into_owned();
let f_name = f.file.file_name().to_string_lossy().into_owned();
let distance = levenshtein(&f_name, &tmp_name);
if distance < 15 {
match is_same_duration(f, tmp_f) {
Ok(true) => {
touched[j] = true;
new_list.push(&tmp_f);
}
Ok(false) => continue,
Err(e) => {
touched[j] = true;
eprintln!("Skipping unreadable file: {e}");
continue;
}
}
}
}
if new_list.len() > 1 {
duplicates.push(new_list);
}
}
Ok(duplicates)
}
fn is_same_duration(f1: &FileInfo, f2: &FileInfo) -> Result<bool, Box<dyn Error>> {
let d1 = get_duration(&f1.file.path())?;
let d2 = get_duration(&f2.file.path())?;
Ok((d1 - d2).abs() == 0.0)
}
fn get_duration(path: &Path) -> Result<f64, Box<dyn Error>> {
let output = Command::new("ffprobe")
.args([
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
])
.arg(path)
.output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
eprintln!("ffprobe failed for {:?}: {}", path, stderr);
return Err(format!("ffprobe exited with status {}", output.status).into());
}
let duration = String::from_utf8(output.stdout)?.trim().parse::<f64>()?;
Ok(duration)
}
fn get_filenames(folder_path: String) -> Result<Vec<FileInfo>, Box<dyn Error>> {
let mut filenames = Vec::new();
for entry in read_dir(folder_path)? {
filenames.push(FileInfo { file: entry? });
}
Ok(filenames)
}
fn get_folder_path() -> Result<String, Box<dyn Error>> {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
return Err("you must provide a folder path".into());
}
Ok(args[1].to_string())
}