diff --git a/.vscode/launch.json b/.vscode/launch.json index 064b5ae..f7871a6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,6 +11,14 @@ "preLaunchTask": "Build", "program": "${workspaceFolder}/bin/Debug/net8.0/win-x64/File-Folder-Helper.dll", "args": [ + "s", + "X", + "D:/Tmp/phares/NewFolder", + "Day-Helper-2025-09-26", + "EAF_INFO*.lo*", + "S1F4 W-Bit", + "yyyy-MM-dd HH:mm:ss,fff", + "10", "s", "X", "A:/6-Other-Large-Z/Linux-Ubuntu-Affirm/etc/nginx/include~B:/6-Other-Large-Z/Linux-Ubuntu-BCHS/etc/nginx/include~J:/6-Other-Large-Z/Linux-Ubuntu-JMLC/etc/nginx/include~P:/6-Other-Large-Z/Linux-Ubuntu-Phares/etc/nginx/include", diff --git a/ADO2025/PI7/Helper-2025-09-26.cs b/ADO2025/PI7/Helper-2025-09-26.cs new file mode 100644 index 0000000..b66d073 --- /dev/null +++ b/ADO2025/PI7/Helper-2025-09-26.cs @@ -0,0 +1,131 @@ +using Microsoft.Extensions.Logging; +using System.Globalization; + +namespace File_Folder_Helper.ADO2025.PI7; + +internal static partial class Helper20250926 { + + internal static void RenameThenFindFirstAndLast(ILogger logger, List args) { + logger.LogInformation(args[0]); + logger.LogInformation(args[1]); + logger.LogInformation(args[2]); + logger.LogInformation(args[3]); + logger.LogInformation(args[4]); + logger.LogInformation(args[5]); + Rename(logger, args); + if (args.Count > 99) { + FindFirstAndLast(logger, args); + } + } + + internal static void Rename(ILogger logger, List args) { + string check; + string[] files; + string[] lines; + string checkFile; + DateTime lastDate; + DateTime firstDate; + string dateFormat = args[4]; + FileInfo[] fileInfoCollection; + string searchPattern = args[2]; + int seconds = int.Parse(args[5]); + string sourceDirectory = Path.GetFullPath(args[0]); + string[] sourceDirectories = Directory.GetDirectories(sourceDirectory, "*", SearchOption.TopDirectoryOnly); + foreach (string directory in sourceDirectories) { + if (!searchPattern.Contains("_") || !searchPattern.Contains("*")) { + throw new Exception($"{nameof(searchPattern)} must contain _ and *"); + } + files = Directory.GetFiles(directory, searchPattern, SearchOption.TopDirectoryOnly); + logger.LogInformation("With search pattern '{SearchPattern}' found {files} file(s)", searchPattern, files.Length); + fileInfoCollection = files.Select(f => new FileInfo(f)).ToArray(); + foreach (FileInfo fileInfo in fileInfoCollection.OrderBy(f => f.LastWriteTime)) { + if (fileInfo.Length == 0 || fileInfo.LastWriteTime > DateTime.Now.AddMinutes(-1)) { + logger.LogInformation("skipped '{FileInfo}'", fileInfo.FullName); + continue; + } + lastDate = DateTime.MinValue; + firstDate = DateTime.MinValue; + try { + lines = File.ReadAllLines(fileInfo.FullName); + foreach (string line in lines) { + if (line.Length < dateFormat.Length) { + continue; + } + check = line[..dateFormat.Length]; + if (!DateTime.TryParseExact(check, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime logDate)) { + continue; + } + if (firstDate == DateTime.MinValue) { + firstDate = logDate; + } + lastDate = logDate; + } + checkFile = Path.Combine(fileInfo.DirectoryName!, $"{searchPattern.Split('*')[0].Replace('_', '~')}---{firstDate:yyyy-MM-dd_HH-mm-ss}---{lastDate:yyyy-MM-dd_HH-mm-ss}.log"); + if (checkFile == fileInfo.FullName || File.Exists(checkFile)) { + logger.LogInformation("skipped '{CheckFile}'", checkFile); + } else { + logger.LogInformation("rename to '{CheckFile}'", checkFile); + File.Move(fileInfo.FullName, checkFile); + } + } catch { + logger.LogInformation("skipped '{FileInfo}'", fileInfo.FullName); + } + } + } + } + + internal static void FindFirstAndLast(ILogger logger, List args) { + string line; + string check; + string[] files; + string[] lines; + DateTime lastDate; + TimeSpan timeSpan; + DateTime firstDate; + string dateFormat = args[4]; + FileInfo[] fileInfoCollection; + string searchString = args[3]; + string searchPattern = args[2]; + int seconds = int.Parse(args[5]); + string sourceDirectory = Path.GetFullPath(args[0]); + string[] sourceDirectories = Directory.GetDirectories(sourceDirectory, "*", SearchOption.TopDirectoryOnly); + foreach (string directory in sourceDirectories) { + if (!searchPattern.Contains("_") || !searchPattern.Contains("*")) { + throw new Exception($"{nameof(searchPattern)} must contain _ and *"); + } + files = Directory.GetFiles(directory, searchPattern, SearchOption.TopDirectoryOnly); + logger.LogInformation("With search pattern '{SearchPattern}' found {files} file(s)", searchPattern, files.Length); + fileInfoCollection = files.Select(f => new FileInfo(f)).ToArray(); + foreach (FileInfo fileInfo in fileInfoCollection.OrderBy(f => f.LastWriteTime)) { + if (fileInfo.Length == 0 || fileInfo.LastWriteTime > DateTime.Now.AddMinutes(-1)) { + logger.LogInformation("skipped '{FileInfo}'", fileInfo.FullName); + continue; + } + lastDate = DateTime.MinValue; + firstDate = DateTime.MinValue; + try { + lines = File.ReadAllLines(fileInfo.FullName); + for (int i = 0; i < lines.Length; i++) { + line = lines[i]; + if (!line.Contains(searchString) || lines[i - 1].Length < dateFormat.Length) { + continue; + } + check = lines[i - 1][..dateFormat.Length]; + if (!DateTime.TryParseExact(check, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime logDate)) { + continue; + } + timeSpan = logDate - lastDate; + if (firstDate == DateTime.MinValue || timeSpan.TotalSeconds > seconds) { + firstDate = logDate; + } + lastDate = logDate; + } + logger.LogInformation("include"); + } catch { + logger.LogInformation("skipped '{FileInfo}'", fileInfo.FullName); + } + } + } + } + +} \ No newline at end of file diff --git a/Day/HelperDay.cs b/Day/HelperDay.cs index 12e0e74..b4d5aa1 100644 --- a/Day/HelperDay.cs +++ b/Day/HelperDay.cs @@ -187,6 +187,8 @@ internal static class HelperDay ADO2025.PI6.Helper20250726.CopyToCombinedEnumAndIndexFormat(logger, args); else if (args[1] == "Day-Helper-2025-09-08") ADO2025.PI7.Helper20250908.DebugProxyPass(logger, args); + else if (args[1] == "Day-Helper-2025-09-26") + ADO2025.PI7.Helper20250926.RenameThenFindFirstAndLast(logger, args); else throw new Exception(appSettings.Company); } diff --git a/File-Folder-Helper.csproj b/File-Folder-Helper.csproj index 744ee4c..ebf0771 100644 --- a/File-Folder-Helper.csproj +++ b/File-Folder-Helper.csproj @@ -18,7 +18,7 @@ - +