From a510019c1d13d96ad1b562fc1765e50846fa9baf Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Sat, 24 Dec 2022 13:30:33 -0700 Subject: [PATCH] Delete-By-Distinct-II --- Delete-By-Distinct/.vscode/launch.json | 4 +- Delete-By-Distinct/DeleteByDistinct.cs | 81 ++++++++++++------- Delete-By-Distinct/Models/AppSettings.cs | 4 +- .../Models/Binder/AppSettings.cs | 2 + .../appsettings.Development.json | 6 +- Delete-By-Distinct/appsettings.json | 1 + 6 files changed, 63 insertions(+), 35 deletions(-) diff --git a/Delete-By-Distinct/.vscode/launch.json b/Delete-By-Distinct/.vscode/launch.json index 3ca5915..6df0522 100644 --- a/Delete-By-Distinct/.vscode/launch.json +++ b/Delete-By-Distinct/.vscode/launch.json @@ -11,9 +11,7 @@ "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceFolder}/bin/Debug/net7.0/win-x64/Delete-By-Distinct.dll", - "args": [ - "s" - ], + "args": [], "env": { "ASPNETCORE_ENVIRONMENT": "Development", }, diff --git a/Delete-By-Distinct/DeleteByDistinct.cs b/Delete-By-Distinct/DeleteByDistinct.cs index fb26110..07bf7c8 100644 --- a/Delete-By-Distinct/DeleteByDistinct.cs +++ b/Delete-By-Distinct/DeleteByDistinct.cs @@ -17,7 +17,6 @@ public class DeleteByDistinct { } if (console is null) { } - string searchPattern = "*"; long ticks = DateTime.Now.Ticks; ILogger? log = Log.ForContext(); Dictionary>> fileSizeToCollection = new(); @@ -25,24 +24,29 @@ public class DeleteByDistinct ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true }; configuration.Update(); log.Information(configuration.RootDirectory); - Work(searchPattern, ticks, log, configuration.RootDirectory, nameof(configuration.RootDirectory), options, fileSizeToCollection); - Work(searchPattern, ticks, log, appSettings.CompareRootDirectory, nameof(appSettings.CompareRootDirectory), options, fileSizeToCollection); + bool compareIsPopulatedAndNotTheSame = !string.IsNullOrEmpty(appSettings.CompareRootDirectory) && appSettings.CompareRootDirectory != configuration.RootDirectory; + Work(appSettings, ticks, log, configuration.RootDirectory, nameof(configuration.RootDirectory), options, fileSizeToCollection, logOnly: compareIsPopulatedAndNotTheSame); + if (compareIsPopulatedAndNotTheSame) + Work(appSettings, ticks, log, appSettings.CompareRootDirectory, nameof(appSettings.CompareRootDirectory), options, fileSizeToCollection, logOnly: false); + _ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(appSettings.CompareRootDirectory); } - private static void Work(string searchPattern, long ticks, ILogger log, string directory, string variable, ProgressBarOptions options, Dictionary>> fileSizeToCollection) + private static void Work(AppSettings appSettings, long ticks, ILogger log, string directory, string variable, ProgressBarOptions options, Dictionary>> fileSizeToCollection, bool logOnly) { string message; string checkName; string deleteLog; int totalSeconds; FileInfo fileInfo; + DateTime checkDate; ProgressBar progressBar; List? fileNames; + ConsoleKey? consoleKey = null; List deletedFiles = new(); List deletedDirectories = new(); Dictionary>? fileTicksToNames; - log.Information($"Gathering files from <{directory}>"); - (string directory, string[] files)[] leftCollection = Shared.Models.Stateless.Methods.IFileHolder.GetFiles(directory, searchPattern).ToArray(); + log.Information($"Gathering {appSettings.SearchPattern} files from <{directory}>"); + (string directory, string[] files)[] leftCollection = Shared.Models.Stateless.Methods.IFileHolder.GetFiles(directory, appSettings.SearchPattern).ToArray(); totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds); message = $") Looking for duplicates from <{directory}> - {totalSeconds} total second(s)"; progressBar = new(leftCollection.Length, message, options); @@ -52,54 +56,71 @@ public class DeleteByDistinct foreach (string file in files) { fileInfo = new(file); + if (fileInfo.Length < 100) + continue; if (!fileSizeToCollection.TryGetValue(fileInfo.Length, out fileTicksToNames)) { fileSizeToCollection.Add(fileInfo.Length, new()); if (!fileSizeToCollection.TryGetValue(fileInfo.Length, out fileTicksToNames)) throw new Exception(); } - if (!fileTicksToNames.TryGetValue(fileInfo.LastWriteTime.Ticks, out fileNames)) + // if (!fileTicksToNames.TryGetValue(fileInfo.LastWriteTime.Ticks, out fileNames)) + // { + // fileTicksToNames.Add(fileInfo.LastWriteTime.Ticks, new()); + // if (!fileTicksToNames.TryGetValue(fileInfo.LastWriteTime.Ticks, out fileNames)) + // throw new Exception(); + // } + checkDate = new DateTime(fileInfo.LastWriteTime.Year, fileInfo.LastWriteTime.Month, fileInfo.LastWriteTime.Day); + if (!fileTicksToNames.TryGetValue(checkDate.Ticks, out fileNames)) { - fileTicksToNames.Add(fileInfo.LastWriteTime.Ticks, new()); - if (!fileTicksToNames.TryGetValue(fileInfo.LastWriteTime.Ticks, out fileNames)) + fileTicksToNames.Add(checkDate.Ticks, new()); + if (!fileTicksToNames.TryGetValue(checkDate.Ticks, out fileNames)) throw new Exception(); } - checkName = fileInfo.Name.ToLower(); + checkName = fileInfo.Name.ToLower(); //.Replace(".jpeg", ".jpg"); if (fileNames.Contains(checkName)) deletedFiles.Add(file); else fileNames.Add(checkName); } } + progressBar.Dispose(); + log.Information(". . ."); deleteLog = $"{ticks}-{variable}-Files.lsv"; - File.WriteAllLines(Path.Combine(directory, deleteLog), deletedFiles); - if (deletedFiles.Any()) + if (!logOnly) + File.WriteAllLines(Path.Combine(directory, deleteLog), deletedFiles); + if (deletedFiles.Any() && !logOnly) { - log.Information($"Ready to delete {deletedFiles.Count} from {variable} file(s)? See <{deleteLog}>"); + log.Information($"Ready to delete {deletedFiles.Count} from {variable} {appSettings.SearchPattern} file(s)? See <{deleteLog}>"); for (int y = 0; y < int.MaxValue; y++) { - log.Information("Press \"Y\" key to delete file(s) or close console to not delete files"); - if (Console.ReadKey().Key == ConsoleKey.Y) + log.Information("Press \"Y\" key to delete file(s), \"N\" key to log file(s) or close console to not delete files"); + consoleKey = Console.ReadKey().Key; + if (consoleKey is ConsoleKey.Y or ConsoleKey.N) break; } log.Information(". . ."); - foreach (string file in deletedFiles) - File.Delete(file); - totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds); - message = $") Looking for empty directories from <{directory}> - {totalSeconds} total second(s)"; - progressBar.Dispose(); - progressBar = new(4, message, options); - for (int i = 1; i < 5; i++) + if (consoleKey is not null && consoleKey.Value == ConsoleKey.Y) { - progressBar.Tick(); - List check = new(); - Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(directory, check); - if (!check.Any()) - break; - deletedDirectories.AddRange(check); + foreach (string file in deletedFiles) + File.Delete(file); + totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds); + message = $") Looking for empty directories from <{directory}> - {totalSeconds} total second(s)"; + progressBar = new(4, message, options); + for (int i = 1; i < 5; i++) + { + progressBar.Tick(); + List check = new(); + Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(directory, check); + if (!check.Any()) + break; + deletedDirectories.AddRange(check); + } + progressBar.Dispose(); + log.Information(". . ."); + deleteLog = $"{ticks + 1}-{variable}-Directories.lsv"; + File.WriteAllLines(Path.Combine(directory, deleteLog), deletedDirectories.Distinct()); } - deleteLog = $"{ticks}-{variable}-Directories.lsv"; - File.WriteAllLines(Path.Combine(directory, deleteLog), deletedDirectories.Distinct()); } } } \ No newline at end of file diff --git a/Delete-By-Distinct/Models/AppSettings.cs b/Delete-By-Distinct/Models/AppSettings.cs index bce0f71..4d8471e 100644 --- a/Delete-By-Distinct/Models/AppSettings.cs +++ b/Delete-By-Distinct/Models/AppSettings.cs @@ -10,15 +10,17 @@ public class AppSettings public string CompareRootDirectory { init; get; } public int MaxDegreeOfParallelism { init; get; } public string OutputExtension { init; get; } + public string SearchPattern { init; get; } public string WorkingDirectoryName { init; get; } [JsonConstructor] - public AppSettings(string company, string compareRootDirectory, int maxDegreeOfParallelism, string outputExtension, string workingDirectoryName) + public AppSettings(string company, string compareRootDirectory, int maxDegreeOfParallelism, string outputExtension, string searchPattern, string workingDirectoryName) { Company = company; CompareRootDirectory = compareRootDirectory; MaxDegreeOfParallelism = maxDegreeOfParallelism; OutputExtension = outputExtension; + SearchPattern = searchPattern; WorkingDirectoryName = workingDirectoryName; } diff --git a/Delete-By-Distinct/Models/Binder/AppSettings.cs b/Delete-By-Distinct/Models/Binder/AppSettings.cs index 5826fc4..a46d348 100644 --- a/Delete-By-Distinct/Models/Binder/AppSettings.cs +++ b/Delete-By-Distinct/Models/Binder/AppSettings.cs @@ -12,6 +12,7 @@ public class AppSettings public string CompareRootDirectory { get; set; } public int? MaxDegreeOfParallelism { get; set; } public string OutputExtension { get; set; } + public string SearchPattern { get; set; } public string WorkingDirectoryName { get; set; } #nullable restore @@ -32,6 +33,7 @@ public class AppSettings appSettings.CompareRootDirectory, appSettings.MaxDegreeOfParallelism.Value, appSettings.OutputExtension, + appSettings.SearchPattern, appSettings.WorkingDirectoryName ); return result; diff --git a/Delete-By-Distinct/appsettings.Development.json b/Delete-By-Distinct/appsettings.Development.json index 4c4884f..2878dc4 100644 --- a/Delete-By-Distinct/appsettings.Development.json +++ b/Delete-By-Distinct/appsettings.Development.json @@ -7,13 +7,17 @@ } }, "MaxDegreeOfParallelism": 6, + "SearchPattern": "*.jpg", "Serilog": { "MinimumLevel": "Debug" }, "Windows": { "Configuration": { "xRootDirectory": "C:/Tmp-x", - "RootDirectory": "D:/Tmp/Phares/Compare/Not-Copy-Copy-4d49b68", + "xxRootDirectory": "D:/Tmp/Phares/Compare/Not-Copy-Copy-4d49b68", + "xxxRootDirectory": "F:/Tmp/Phares/Compare", + "xxxxRootDirectory": "E:/Tmp/Phares/Compare", + "RootDirectory": "F:/Tmp/Phares/Compare/Images-4d49b68", "VerifyToSeason": [ ". 2000", ". 2001", diff --git a/Delete-By-Distinct/appsettings.json b/Delete-By-Distinct/appsettings.json index ec3e650..fc41f08 100644 --- a/Delete-By-Distinct/appsettings.json +++ b/Delete-By-Distinct/appsettings.json @@ -11,6 +11,7 @@ } }, "MaxDegreeOfParallelism": 6, + "SearchPattern": "*.jpg", "Serilog": { "Using": [ "Serilog.Sinks.Console",