diff --git a/Copy-Distinct/CopyDistinct.cs b/Copy-Distinct/CopyDistinct.cs index 73f1484..61531ff 100644 --- a/Copy-Distinct/CopyDistinct.cs +++ b/Copy-Distinct/CopyDistinct.cs @@ -84,6 +84,8 @@ public class CopyDistinct { List<(FileHolder, string)> results = new(); string checkFile; + string directory; + FileInfo fileInfo; int directoryIndex; FileHolder fileHolder; List distinct = new(); @@ -104,19 +106,67 @@ public class CopyDistinct continue; } (_, directoryIndex) = Shared.Models.Stateless.Methods.IPath.GetDirectoryNameAndIndex(_PropertyConfiguration.ResultAllInOneSubdirectoryLength, fileHolder.NameWithoutExtension); - checkFile = Path.Combine(_FileGroups[_PropertyConfiguration.ResultContent][directoryIndex], fileHolder.Name); + directory = _FileGroups[_PropertyConfiguration.ResultContent][directoryIndex]; + checkFile = Path.Combine(directory, $"{fileHolder.NameWithoutExtension}{fileHolder.ExtensionLowered}"); if (distinct.Contains(checkFile)) + { + for (int i = 1; i < int.MaxValue; i++) + { + fileInfo = new(checkFile); + if (fileHolder.Length != fileInfo.Length || fileHolder.LastWriteTime != fileInfo.LastWriteTime) + checkFile = Path.Combine(directory, $"{fileHolder.NameWithoutExtension}.{i}why{fileHolder.ExtensionLowered}"); + else + checkFile = Path.Combine(directory, $"{fileHolder.NameWithoutExtension}.{i}dup{fileHolder.ExtensionLowered}"); + if (distinct.Contains(checkFile)) + continue; + distinct.Add(checkFile); + results.Add(new(fileHolder, checkFile)); + break; + } continue; + } distinct.Add(checkFile); results.Add(new(fileHolder, checkFile)); } return results; } - private static List Copy(ILogger log, List<(FileHolder, string)> toDoCollection) + private static List Copy(ProgressBar progressBar, List<(FileHolder, string)> toDoCollection) { List results = new(); + FileInfo fileInfo; + foreach ((FileHolder fileHolder, string to) in toDoCollection) + { + progressBar.Tick(); + fileInfo = new(to); + if (fileInfo.Exists) + { + if (fileHolder.Length != fileInfo.Length || fileHolder.LastWriteTime != fileInfo.LastWriteTime) + fileInfo.Delete(); + continue; + } + results.Add(fileHolder.NameWithoutExtension); + try + { File.Copy(fileHolder.FullName, to); } + catch (Exception) { } + } + return results; + } + + private List CopyDistinctFilesInDirectories(ILogger log) + { + List results = new(); + ProgressBar progressBar; ConsoleKey? consoleKey = null; + const string fileSearchFilter = "*"; + string message = nameof(CopyDistinct); + const string directorySearchFilter = "*"; + List filesCollection = Shared.Models.Stateless.Methods.IDirectory.GetFilesCollection(_PropertyConfiguration.RootDirectory, directorySearchFilter, fileSearchFilter); + (_, List allFiles) = Get(filesCollection); + ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true }; + progressBar = new(allFiles.Count, message, options); + List<(FileHolder, string)> toDoCollection = GetToDoCollection(progressBar, allFiles); + progressBar.Dispose(); log.Information($"Ready to Copy {toDoCollection.Count} file(s)?"); for (int y = 0; y < int.MaxValue; y++) { @@ -130,34 +180,12 @@ public class CopyDistinct log.Information("Nothing copied!"); else { - foreach ((FileHolder fileHolder, string to) in toDoCollection) - { - if (File.Exists(to)) - continue; - results.Add(fileHolder.NameWithoutExtension); - try - { File.Copy(fileHolder.FullName, to); } - catch (Exception) { } - } + progressBar = new(allFiles.Count, message, options); + results.AddRange(Copy(progressBar, toDoCollection)); + progressBar.Dispose(); log.Information("Done copying"); } return results; } - private List CopyDistinctFilesInDirectories(ILogger log) - { - List results = new(); - const string fileSearchFilter = "*"; - string message = nameof(CopyDistinct); - const string directorySearchFilter = "*"; - List filesCollection = Shared.Models.Stateless.Methods.IDirectory.GetFilesCollection(_PropertyConfiguration.RootDirectory, directorySearchFilter, fileSearchFilter); - (_, List allFiles) = Get(filesCollection); - ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true }; - ProgressBar progressBar = new(allFiles.Count, message, options); - List<(FileHolder, string)> toDoCollection = GetToDoCollection(progressBar, allFiles); - progressBar.Dispose(); - results.AddRange(Copy(log, toDoCollection)); - return results; - } - }