commit 9ab550a1cb21a759b05377e21d616d9b172ce381 Author: mimu Date: Tue Oct 13 17:08:05 2026 +0200 v0.1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..5dfad0c --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "dupes" +version = "0.1.0" +dependencies = [ + "strsim", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5ac314c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "dupes" +version = "0.1.0" +edition = "2024" + +[dependencies] +strsim = "0.11" diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..42793c1 --- /dev/null +++ b/readme.md @@ -0,0 +1,3 @@ +# dupes + +Find duplicates files in folder. diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d5eff78 --- /dev/null +++ b/src/main.rs @@ -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> { + 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>) -> Result<(), Box> { + 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) -> Result>, Box> { + 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> { + 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> { + 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::()?; + + Ok(duration) +} + +fn get_filenames(folder_path: String) -> Result, Box> { + let mut filenames = Vec::new(); + + for entry in read_dir(folder_path)? { + filenames.push(FileInfo { file: entry? }); + } + + Ok(filenames) +} + +fn get_folder_path() -> Result> { + let args: Vec = env::args().collect(); + + if args.len() < 2 { + return Err("you must provide a folder path".into()); + } + + Ok(args[1].to_string()) +}