Ready to test
This commit is contained in:
71
Helpers/HelperLogMerge.cs
Normal file
71
Helpers/HelperLogMerge.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperLogMerge
|
||||
{
|
||||
|
||||
internal static void LogMerge(string argsZero)
|
||||
{
|
||||
int hour;
|
||||
string day;
|
||||
string logFile;
|
||||
FileInfo fileInfo;
|
||||
string[] segments;
|
||||
string checkDirectory;
|
||||
string format = "yyyyMMdd";
|
||||
string segment1 = string.Empty;
|
||||
string sourceFileNameWithoutExtension;
|
||||
List<string> lines = new();
|
||||
List<string> moveFiles = new();
|
||||
DateTime dateTime = DateTime.Now.AddMinutes(2);
|
||||
string[] sourceFiles = Directory.GetFiles(argsZero, "*.log", SearchOption.TopDirectoryOnly);
|
||||
Dictionary<string, Dictionary<int, string[]>> keyValuePairs = new();
|
||||
foreach (string sourceFile in sourceFiles)
|
||||
{
|
||||
sourceFileNameWithoutExtension = Path.GetFileNameWithoutExtension(sourceFile);
|
||||
if (sourceFileNameWithoutExtension.Split('-').Length > 2)
|
||||
continue;
|
||||
if (sourceFileNameWithoutExtension.StartsWith(dateTime.ToString(format)))
|
||||
continue;
|
||||
fileInfo = new FileInfo(sourceFile);
|
||||
if (fileInfo.Length == 0)
|
||||
moveFiles.Add(sourceFile);
|
||||
day = sourceFileNameWithoutExtension[..8];
|
||||
if (!keyValuePairs.ContainsKey(day))
|
||||
keyValuePairs.Add(day, new Dictionary<int, string[]>());
|
||||
if (sourceFileNameWithoutExtension.Substring(8, 1) == "_")
|
||||
continue;
|
||||
segments = sourceFileNameWithoutExtension.Split('_');
|
||||
if (!string.IsNullOrEmpty(segment1) && segment1 != segments[1])
|
||||
continue;
|
||||
segment1 = segments[1];
|
||||
hour = int.Parse(sourceFileNameWithoutExtension.Substring(8, 2));
|
||||
if (keyValuePairs[day].ContainsKey(hour))
|
||||
continue;
|
||||
keyValuePairs[day].Add(hour, File.ReadAllLines(sourceFile));
|
||||
moveFiles.Add(sourceFile);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(segment1))
|
||||
{
|
||||
foreach (KeyValuePair<string, Dictionary<int, string[]>> keyValuePair in keyValuePairs)
|
||||
{
|
||||
lines.Clear();
|
||||
dateTime = DateTime.ParseExact(keyValuePair.Key, format, CultureInfo.InvariantCulture);
|
||||
foreach (KeyValuePair<int, string[]> item in keyValuePair.Value.OrderBy(l => l.Key))
|
||||
lines.AddRange(item.Value);
|
||||
logFile = string.Concat(argsZero, @"\", dateTime.ToString("yyyy-MM-dd"), "_", segment1, ".log");
|
||||
File.WriteAllLines(logFile, lines);
|
||||
if (dateTime.Hour == 0 && dateTime.Minute == 0 && dateTime.Second == 0 && dateTime.Millisecond == 0)
|
||||
_ = dateTime.AddHours(23).AddMinutes(59).AddSeconds(59);
|
||||
File.SetLastWriteTime(logFile, dateTime);
|
||||
}
|
||||
checkDirectory = string.Concat(argsZero, @"\_ Merged");
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
foreach (string moveFile in moveFiles.Distinct())
|
||||
File.Move(moveFile, string.Concat(checkDirectory, @"\", Path.GetFileName(moveFile)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user