diff --git a/ADO2024/PI4/Helper-2024-12-24.cs b/ADO2024/PI4/Helper-2024-12-24.cs index d37479a..8bb5602 100644 --- a/ADO2024/PI4/Helper-2024-12-24.cs +++ b/ADO2024/PI4/Helper-2024-12-24.cs @@ -161,47 +161,6 @@ internal static partial class Helper20241224 } } - private static ReadOnlyDictionary> GetKeyValuePairs(string directory, string searchPattern, string split) - { - string key; - List? collection; - Dictionary> results = []; - string[] files = Directory.GetFiles(directory, searchPattern, SearchOption.TopDirectoryOnly); - FileInfo[] fileInfoCollection = files.Select(l => new FileInfo(l)).ToArray(); - foreach (FileInfo fileInfo in fileInfoCollection.OrderBy(l => l.LastWriteTime)) - { - key = fileInfo.Name.Split(split)[0]; - if (!results.TryGetValue(key, out collection)) - { - results.Add(key, []); - if (!results.TryGetValue(key, out collection)) - throw new Exception(); - } - collection.Add(fileInfo); - } - return results.AsReadOnly(); - } - - private static void DeleteFirst(string directory) - { - string checkFile; - FileInfo fileInfo; - ReadOnlyDictionary> keyValuePairs = GetKeyValuePairs(directory, "*.tar", $"-{DateTime.Now.Year}-"); - foreach (KeyValuePair> keyValuePair in keyValuePairs) - { - if (keyValuePair.Value.Count < 3) - continue; - for (int i = 1; i < keyValuePair.Value.Count - 1; i++) - { - fileInfo = keyValuePair.Value[i]; - checkFile = Path.Combine($"{fileInfo.Directory}-Delete", fileInfo.Name); - if (File.Exists(checkFile)) - continue; - File.Move(fileInfo.FullName, checkFile); - } - } - } - internal static void Compare(ILogger logger, List args) { string host = args[2]; @@ -209,7 +168,6 @@ internal static partial class Helper20241224 string format = NginxFileSystem.GetFormat(); TimeZoneInfo timeZoneInfo = TimeZoneInfo.Local; string compareDirectory = Path.GetFullPath(args[0]); - DeleteFirst(compareDirectory); logger.LogInformation("Comparing files on {host}", host); ReadOnlyCollection records = GetRecord(format, timeZoneInfo, host, new([rootDirectoryName]), compareDirectory); #if ShellProgressBar diff --git a/ADO2025/PI4/Helper-2025-01-01.cs b/ADO2025/PI4/Helper-2025-01-01.cs new file mode 100644 index 0000000..7299efa --- /dev/null +++ b/ADO2025/PI4/Helper-2025-01-01.cs @@ -0,0 +1,61 @@ +using Microsoft.Extensions.Logging; +using System.Collections.ObjectModel; + +namespace File_Folder_Helper.ADO2025.PI4; + +internal static partial class Helper20250101 +{ + + private static ReadOnlyDictionary> GetKeyValuePairs(string directory, string searchPattern, string split) + { + string key; + List? collection; + Dictionary> results = []; + string[] files = Directory.GetFiles(directory, searchPattern, SearchOption.TopDirectoryOnly); + FileInfo[] fileInfoCollection = files.Select(l => new FileInfo(l)).ToArray(); + foreach (FileInfo fileInfo in fileInfoCollection.OrderBy(l => l.LastWriteTime)) + { + key = fileInfo.Name.Split(split)[0]; + if (!results.TryGetValue(key, out collection)) + { + results.Add(key, []); + if (!results.TryGetValue(key, out collection)) + throw new Exception(); + } + collection.Add(fileInfo); + } + return results.AsReadOnly(); + } + + private static void MoveToDelete(ILogger logger, string appendage, ReadOnlyDictionary> keyValuePairs) + { + string checkFile; + FileInfo fileInfo; + foreach (KeyValuePair> keyValuePair in keyValuePairs) + { + if (keyValuePair.Value.Count < 3) + continue; + for (int i = 1; i < keyValuePair.Value.Count - 1; i++) + { + fileInfo = keyValuePair.Value[i]; + checkFile = Path.Combine($"{fileInfo.Directory}{appendage}", fileInfo.Name); + if (File.Exists(checkFile)) + continue; + logger.LogInformation("Moving <{fileInfo.FullName}> to <{checkFile}>", fileInfo.FullName, checkFile); + File.Move(fileInfo.FullName, checkFile); + } + } + } + + internal static void MoveToDelete(ILogger logger, List args) + { + string split = args[3]; + string appendage = args[4]; + string searchPattern = args[2]; + string compareDirectory = Path.GetFullPath(args[0]); + ReadOnlyDictionary> keyValuePairs = GetKeyValuePairs(compareDirectory, searchPattern, split); + logger.LogInformation("KeyValuePairs: {keyValuePairs}", keyValuePairs.Count); + MoveToDelete(logger, appendage, keyValuePairs); + } + +} \ No newline at end of file diff --git a/Day/HelperDay.cs b/Day/HelperDay.cs index 8de337b..c33ec54 100644 --- a/Day/HelperDay.cs +++ b/Day/HelperDay.cs @@ -125,6 +125,8 @@ internal static class HelperDay ADO2024.PI4.Helper20241217.Backup(logger, args); else if (args[1] == "Day-Helper-2024-12-24") ADO2024.PI4.Helper20241224.Compare(logger, args); + else if (args[1] == "Day-Helper-2025-01-01") + ADO2025.PI4.Helper20250101.MoveToDelete(logger, args); else throw new Exception(appSettings.Company); }