From f380a7ec20b41de7b85cbdd296d6aa5a6b842b90 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Sun, 5 Feb 2023 10:53:03 -0700 Subject: [PATCH] Rename questions --- Property/Models/Configuration.cs | 2 - Rename/Rename.cs | 84 ++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 21 deletions(-) diff --git a/Property/Models/Configuration.cs b/Property/Models/Configuration.cs index 4e00d7b..f76a9b4 100644 --- a/Property/Models/Configuration.cs +++ b/Property/Models/Configuration.cs @@ -108,8 +108,6 @@ public class Configuration : Shared.Models.Properties.IPropertyConfiguration throw new NullReferenceException(nameof(propertyConfiguration.ValidMetadataExtensions)); if (propertyConfiguration.VerifyToSeason is null || !propertyConfiguration.VerifyToSeason.Any()) throw new NullReferenceException(nameof(propertyConfiguration.VerifyToSeason)); - if (Path.GetPathRoot(propertyConfiguration.RootDirectory) == propertyConfiguration.RootDirectory) - throw new NullReferenceException(nameof(propertyConfiguration.RootDirectory)); if (propertyConfiguration is null) throw new NullReferenceException(nameof(propertyConfiguration)); if (string.IsNullOrEmpty(propertyConfiguration.DateGroup)) diff --git a/Rename/Rename.cs b/Rename/Rename.cs index d027238..3571f45 100644 --- a/Rename/Rename.cs +++ b/Rename/Rename.cs @@ -52,7 +52,7 @@ public class Rename Rename2000(matchNginxCollection); else if (matchNginxCollection.Any()) { - List lines = RenameFilesInDirectories(options, matchNginxCollection); + List lines = RenameFilesInDirectories(log, options, matchNginxCollection); File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines); if (comparePathRoot != Path.GetPathRoot(matchNginxCollection[0].ConvertedPath)) _ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(comparePathRoot); @@ -261,13 +261,15 @@ public class Rename } } - private void CopyInstead(List<(FileHolder, string)> toDoCollection) + private List CopyInstead(ILogger log, List<(FileHolder, string)> verifiedToDoCollection) { + List results = new(); string copyTo; string? directory; + ConsoleKey? consoleKey = null; List distinctDirectories = new(); List<(FileHolder, string)> copyCollection = new(); - foreach ((FileHolder fileHolder, string to) in toDoCollection) + foreach ((FileHolder fileHolder, string to) in verifiedToDoCollection) { copyTo = $"{_AppSettings.CopyTo}{to[1..]}"; directory = Path.GetDirectoryName(copyTo); @@ -279,15 +281,58 @@ public class Rename distinctDirectories.Add(directory); } CreateDirectories(distinctDirectories); - foreach ((FileHolder fileHolder, string to) in copyCollection) + log.Information($"Ready to Copy {verifiedToDoCollection.Count} file(s)?"); + for (int y = 0; y < int.MaxValue; y++) { - if (File.Exists(to)) - continue; - File.Copy(fileHolder.FullName, to); + log.Information("Press \"Y\" key to copy file(s), \"N\" key to log file(s) or close console to not copy files"); + consoleKey = System.Console.ReadKey().Key; + if (consoleKey is ConsoleKey.Y or ConsoleKey.N) + break; } + log.Information(". . ."); + if (consoleKey is null || consoleKey.Value != ConsoleKey.Y) + log.Information("Nothing Copied!"); + else + { + foreach ((FileHolder fileHolder, string to) in verifiedToDoCollection) + { + results.Add(fileHolder.NameWithoutExtension); + File.Copy(fileHolder.FullName, to); + } + log.Information("Done Copying"); + } + return results; } - private List RenameFilesInDirectories(ProgressBarOptions options, MatchNginx[] matchNginxCollection) + private static List Move(ILogger log, List<(FileHolder, string)> verifiedToDoCollection) + { + List results = new(); + ConsoleKey? consoleKey = null; + log.Information($"Ready to Move {verifiedToDoCollection.Count} file(s)?"); + for (int y = 0; y < int.MaxValue; y++) + { + log.Information("Press \"Y\" key to move file(s), \"N\" key to log file(s) or close console to not move files"); + consoleKey = System.Console.ReadKey().Key; + if (consoleKey is ConsoleKey.Y or ConsoleKey.N) + break; + } + log.Information(". . ."); + if (consoleKey is null || consoleKey.Value != ConsoleKey.Y) + log.Information("Nothing moved!"); + else + { + foreach ((FileHolder fileHolder, string to) in verifiedToDoCollection) + { + results.Add(fileHolder.NameWithoutExtension); + File.Move(fileHolder.FullName, to); + File.WriteAllText($"{to}.id", $"{to}{Environment.NewLine}{fileHolder.FullName}"); + } + log.Information("Done Moving"); + } + return results; + } + + private List RenameFilesInDirectories(ILogger log, ProgressBarOptions options, MatchNginx[] matchNginxCollection) { List results = new(); string[] files; @@ -296,6 +341,7 @@ public class Rename ProgressBar progressBar; List distinctCollection = new(); List<(FileHolder, string)> toDoCollection; + List<(FileHolder, string)> verifiedToDoCollection; List allFiles = GetAllFiles(matchNginxCollection); for (int i = 1; i < 3; i++) { @@ -315,19 +361,19 @@ public class Rename distinctCount = distinctCollection.Count; if (toDoCollection.Count != distinctCount) continue; - if (!string.IsNullOrEmpty(_AppSettings.CopyTo)) - CopyInstead(toDoCollection); - else + verifiedToDoCollection = new(); + foreach ((FileHolder fileHolder, string to) in toDoCollection) { - foreach ((FileHolder fileHolder, string to) in toDoCollection) - { - results.Add(fileHolder.NameWithoutExtension); - if (File.Exists(to)) - continue; - File.Move(fileHolder.FullName, to); - File.WriteAllText($"{to}.id", $"{to}{Environment.NewLine}{fileHolder.FullName}"); - } + if (File.Exists(to)) + continue; + verifiedToDoCollection.Add(new(fileHolder, to)); } + if (!verifiedToDoCollection.Any()) + continue; + if (string.IsNullOrEmpty(_AppSettings.CopyTo)) + results.AddRange(Move(log, toDoCollection)); + else + results.AddRange(CopyInstead(log, toDoCollection)); progressBar.Dispose(); } return results;