diff --git a/.vscode/launch.json b/.vscode/launch.json index 0a8ea35..faa7941 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,8 +12,17 @@ "program": "${workspaceFolder}/bin/Debug/net8.0/win-x64/File-Folder-Helper.dll", "args": [ "s", - "K", - ".kanbn" + "X", + "D:/5-Other-Small/Free-File-Sync", + "Day-Helper-2024-08-05", + "*.ffs_gui", + "lines.md", + "C:/Users/phares/AppData/Roaming/FreeFileSync/GlobalSettings.xml", + "5555", + "6666", + "7777", + "8888", + "9999" ], "cwd": "${workspaceFolder}", "console": "integratedTerminal", diff --git a/Day/2024-Q3/Helper-2024-08-05.cs b/Day/2024-Q3/Helper-2024-08-05.cs new file mode 100644 index 0000000..1260cca --- /dev/null +++ b/Day/2024-Q3/Helper-2024-08-05.cs @@ -0,0 +1,74 @@ +using Microsoft.Extensions.Logging; + +namespace File_Folder_Helper.Day; + +internal static partial class Helper20240805 +{ + + private static void RenameFiles(ILogger logger, string sourceDirectory, string[] files, string[] lines, string globalSettingsFile) + { + string to; + string old; + string from; + string what; + string concat; + string checkFile; + string extension; + string[] matches; + string[] segments; + string environment; + List distinct = []; + string globalSettingsLines = File.ReadAllText(globalSettingsFile); + foreach (string line in lines) + { + segments = line.Split('|'); + if (segments.Length != 7) + { + logger.LogWarning("Wrong length!"); + continue; + } + to = segments[5].Trim(); + old = segments[1].Trim(); + from = segments[4].Trim(); + what = segments[3].Trim(); + environment = segments[2].Trim(); + concat = string.Concat(environment, '-', what, '-', from, '-', to); + if (distinct.Contains(concat)) + { + logger.LogWarning("Not distinct <{concat}>!", concat); + continue; + } + distinct.Add(concat); + globalSettingsLines = globalSettingsLines.Replace(old, concat); + matches = (from l in files where l.Contains(old) select l).ToArray(); + if (matches.Length != 1) + { + logger.LogWarning("matches == {matchesLength} <{concat}>!", matches.Length, concat); + continue; + } + extension = Path.GetExtension(matches[0]); + checkFile = Path.Combine(sourceDirectory, $"{concat}{extension}"); + File.Move(matches[0], checkFile); + } + File.WriteAllText(globalSettingsFile, globalSettingsLines); + } + + internal static void RenameFiles(ILogger logger, List args) + { + string sourceDirectory = Path.GetFullPath(args[0]); + string globalSettingsFile = Path.GetFullPath(args[4]); + string[] files = Directory.GetFiles(sourceDirectory, args[2], SearchOption.TopDirectoryOnly); + string checkFile = Path.Combine(sourceDirectory, args[3]); + if (files.Length == 0 || !File.Exists(checkFile)) + logger.LogWarning("No found!"); + else + { + string[] lines = File.ReadAllLines(checkFile); + if (lines.Length == 0) + logger.LogWarning("No lines!"); + else + RenameFiles(logger, sourceDirectory, files, lines, globalSettingsFile); + } + } + +} \ No newline at end of file diff --git a/Day/HelperDay.cs b/Day/HelperDay.cs index b5cfbe4..dd35a73 100644 --- a/Day/HelperDay.cs +++ b/Day/HelperDay.cs @@ -85,6 +85,8 @@ internal static class HelperDay Day.Helper20240724.CopyDirectories(logger, args); else if (args[1] == "Day-Helper-2024-07-28") Day.Helper20240728.DownloadSslCertificates(logger, args); + else if (args[1] == "Day-Helper-2024-08-05") + Day.Helper20240805.RenameFiles(logger, args); else throw new Exception(appSettings.Company); }