diff --git a/RedundancyFinder/Finder.cs b/RedundancyFinder/Finder.cs index 2519620..28b1e99 100644 --- a/RedundancyFinder/Finder.cs +++ b/RedundancyFinder/Finder.cs @@ -10,6 +10,8 @@ namespace RedundancyFinder string[] extensions; + List ignorePaths = new List(); + public event EventHandler? DirectoryError; public event EventHandler? FileError; public event EventHandler? FileFound; @@ -17,6 +19,7 @@ namespace RedundancyFinder public event EventHandler? ProcessingFile; public void FindRedundancies(string[] paths, string[] extensions) { + redundancies?.Values.SelectMany(x => x.Paths).ToList().ForEach(x => ignorePaths.Add(x)); this.extensions = extensions; foreach (var path in paths) { @@ -70,6 +73,11 @@ namespace RedundancyFinder private void ProcessFile(string filePath) { + if(ignorePaths.Contains(filePath)) + { + return; + } + if (!extensions.Contains(Path.GetExtension(filePath))) { return; @@ -78,37 +86,32 @@ namespace RedundancyFinder TaskStarted?.Invoke(this, filePath); ProcessingFile?.Invoke(this, new ProcessingFileEventArgs() { Path = filePath }); - try - { + try + { - var fileHash = ComputeFileHash(filePath); - if (fileHash == null) - { - return; - } - long fileSize = new FileInfo(filePath).Length; - lock (redundancies) - { - if (redundancies.ContainsKey(fileHash)) - { - return; - } + var fileHash = ComputeFileHash(filePath); + if (fileHash == null) + { + return; + } + long fileSize = new FileInfo(filePath).Length; + lock (redundancies) + { + if (!redundancies.ContainsKey(fileHash)) + { + var redundancy = new Redundancy() { Hash = fileHash, FileSize = fileSize }; + redundancies.Add(fileHash, redundancy); + } + redundancies[fileHash].Paths.Add(filePath); + } + FileFound?.Invoke(this, new FileFoundEventArgs(filePath, fileHash, fileSize)); + } + catch (Exception ex) + { + FileError?.Invoke(this, new FileErrorEventArgs() { Exception = ex, Path = filePath }); + } - if (!redundancies.ContainsKey(fileHash)) - { - var redundancy = new Redundancy() { Hash = fileHash, FileSize = fileSize }; - redundancies.Add(fileHash, redundancy); - } - redundancies[fileHash].Paths.Add(filePath); - } - FileFound?.Invoke(this, new FileFoundEventArgs(filePath, fileHash, fileSize)); - } - catch (Exception ex) - { - FileError?.Invoke(this, new FileErrorEventArgs() { Exception = ex, Path = filePath }); - } - - } + } private static string ComputeFileHash(string filePath) { diff --git a/RedundancyFinderCLI/FinderCommand.cs b/RedundancyFinderCLI/FinderCommand.cs index f5d6dd1..64a7bbb 100644 --- a/RedundancyFinderCLI/FinderCommand.cs +++ b/RedundancyFinderCLI/FinderCommand.cs @@ -50,6 +50,31 @@ namespace RedundancyFinderCLI SaveRedundancies(finder, settings.OutputPath); }; + // Load existing redundancies if the output file exists + if (File.Exists(settings.OutputPath)) + { + try + { + var existingData = File.ReadAllText(settings.OutputPath); + var existingRedundancies = JsonConvert.DeserializeObject>(existingData); + + if (existingRedundancies != null) + { + foreach (var entry in existingRedundancies) + { + finder.Redundancies[entry.Key] = entry.Value; + } + + WriteLine($"[yellow]Resumed from existing output file: [/]'{settings.OutputPath}'"); + WriteLine($"[yellow]Loaded {finder.Redundancies.Count} redundancies from the file.[/]"); + } + } + catch (Exception ex) + { + WriteLine($"[red]Failed to load existing output file: {ex.Message}[/]"); + } + } + finder.FileError += (sender, e) => { if (e.Exception is UnauthorizedAccessException) @@ -168,6 +193,7 @@ namespace RedundancyFinderCLI } } + private void WriteLine(string v) { string now = Markup.Escape($"[{DateTime.Now.ToString("HH:mm:ss")}]");