Second round of MoveWaferCounterToArchive
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace File_Folder_Helper.Day.Q32024;
|
||||
|
||||
@ -31,7 +33,9 @@ internal static partial class Helper20240828
|
||||
if (cassetteIdSegments.Length <= 3)
|
||||
result = new Record(null, null, null, null, null, i, null, null);
|
||||
else
|
||||
result = new Record(cassetteIdSegments[2],
|
||||
{
|
||||
string cassetteId = Regex.Replace(cassetteIdSegments[2], @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]", "_").Split('\r')[0].Split('\n')[0];
|
||||
result = new Record(cassetteId,
|
||||
new(cassetteIdSegments),
|
||||
DateTime.Parse(matches[0][0]),
|
||||
matches[0][1],
|
||||
@ -39,6 +43,7 @@ internal static partial class Helper20240828
|
||||
i,
|
||||
lastDate,
|
||||
new(matches));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@ -77,14 +82,16 @@ internal static partial class Helper20240828
|
||||
Record record;
|
||||
string[] lines;
|
||||
string[] logFiles = Directory.GetFiles(logDirectory, logSearchPattern, SearchOption.TopDirectoryOnly);
|
||||
foreach (string logFile in logFiles)
|
||||
if (logFiles.Length > 0)
|
||||
{ }
|
||||
foreach (string logFile in new List<string>()) // logFiles)
|
||||
{
|
||||
lines = File.ReadAllLines(logFile);
|
||||
for (int i = lines.Length - 1; i >= 0; i--)
|
||||
{
|
||||
record = GetRecord(lines, i);
|
||||
i = record.I;
|
||||
if (record.CassetteId is null || record.CassetteSegments is null || record.Date is null || record.Employee is null || record.EquipmentSegments is null)
|
||||
if (record.CassetteId is null || record.CassetteSegments is null || record.Date is null || record.Employee is null || record.EquipmentSegments is null || record.Matches is null)
|
||||
{
|
||||
if (i < 4)
|
||||
break;
|
||||
@ -114,7 +121,7 @@ internal static partial class Helper20240828
|
||||
ReadOnlyCollection<Record> records = GetRecords(logDirectory, logSearchPattern);
|
||||
foreach (Record record in records)
|
||||
{
|
||||
if (record.CassetteId is null || record.CassetteSegments is null || record.Date is null || record.Employee is null || record.EquipmentSegments is null)
|
||||
if (record.CassetteId is null || record.CassetteSegments is null || record.Date is null || record.Employee is null || record.EquipmentSegments is null || record.Matches is null)
|
||||
continue;
|
||||
timeSpan = TimeSpan.FromTicks(record.Date.Value.Ticks);
|
||||
totalMinutes = (int)Math.Floor(timeSpan.TotalMinutes);
|
||||
@ -132,37 +139,72 @@ internal static partial class Helper20240828
|
||||
|
||||
internal static void MoveWaferCounterToArchive(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
Record record;
|
||||
string keyFile;
|
||||
string[] lines;
|
||||
int totalMinutes;
|
||||
string checkFile;
|
||||
TimeSpan timeSpan;
|
||||
string? checkDirectory;
|
||||
string weekOfYear;
|
||||
string checkDirectory;
|
||||
string logDateFormat = args[3];
|
||||
string wcSearchPattern = args[5];
|
||||
string logSearchPattern = args[2];
|
||||
ReadOnlyCollection<Record>? records;
|
||||
string logDirectory = Path.GetFullPath(args[0]);
|
||||
string sourceDirectory = Path.GetFullPath(args[4]);
|
||||
string archiveDirectory = Path.GetFullPath(args[6]);
|
||||
Calendar calendar = new CultureInfo("en-US").Calendar;
|
||||
Dictionary<int, ReadOnlyCollection<Record>> keyValuePairs = GetKeyValuePairs(logSearchPattern, logDirectory);
|
||||
logger.LogInformation("Mapped {keyValuePairs}(s)", keyValuePairs.Count);
|
||||
FileInfo[] collection = Directory.GetFiles(sourceDirectory, wcSearchPattern, SearchOption.AllDirectories).Select(l => new FileInfo(l)).ToArray();
|
||||
logger.LogInformation("Found {collection}(s)", collection.Length);
|
||||
foreach (FileInfo fileInfo in collection)
|
||||
{
|
||||
timeSpan = TimeSpan.FromTicks(fileInfo.LastWriteTime.Ticks);
|
||||
totalMinutes = (int)Math.Floor(timeSpan.TotalMinutes);
|
||||
if (!keyValuePairs.TryGetValue(totalMinutes, out records))
|
||||
if (fileInfo.DirectoryName is null || !fileInfo.DirectoryName.Contains('-'))
|
||||
continue;
|
||||
if (records.Count != 1)
|
||||
keyFile = $"{fileInfo.FullName}.txt";
|
||||
if (!File.Exists(keyFile))
|
||||
continue;
|
||||
checkDirectory = Path.Combine(logDirectory, timeSpan.ToString(logDateFormat));
|
||||
if (string.IsNullOrEmpty(checkDirectory))
|
||||
lines = File.ReadAllLines(keyFile);
|
||||
if (lines.Length != 1)
|
||||
continue;
|
||||
checkDirectory = Path.Combine(fileInfo.DirectoryName, lines[0]);
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
checkFile = Path.Combine(checkDirectory, fileInfo.Name);
|
||||
if (File.Exists(checkFile))
|
||||
continue;
|
||||
File.Move(fileInfo.FullName, checkFile);
|
||||
File.Delete(keyFile);
|
||||
}
|
||||
foreach (FileInfo fileInfo in collection)
|
||||
{
|
||||
if (fileInfo.DirectoryName is null || fileInfo.DirectoryName.Contains('-'))
|
||||
continue;
|
||||
timeSpan = TimeSpan.FromTicks(fileInfo.LastWriteTime.Ticks);
|
||||
totalMinutes = (int)Math.Floor(timeSpan.TotalMinutes);
|
||||
if (!keyValuePairs.TryGetValue(totalMinutes, out records))
|
||||
continue;
|
||||
if (records.Count != 1)
|
||||
continue;
|
||||
record = records[0];
|
||||
if (record.CassetteId is null || record.CassetteSegments is null || record.Date is null || record.Employee is null || record.EquipmentSegments is null || record.Matches is null)
|
||||
continue;
|
||||
weekOfYear = $"{record.Date.Value.Year}_Week_{calendar.GetWeekOfYear(record.Date.Value, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
|
||||
checkDirectory = Path.Combine(archiveDirectory,
|
||||
string.Join('-', record.EquipmentSegments.Reverse()),
|
||||
weekOfYear,
|
||||
record.Date.Value.ToString("yyyy-MM-dd"),
|
||||
record.CassetteId);
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
checkFile = Path.Combine(checkDirectory, fileInfo.Name);
|
||||
if (File.Exists(checkFile))
|
||||
continue;
|
||||
File.Move(fileInfo.FullName, checkFile);
|
||||
lines = record.Matches.Select(l => string.Join(',', l)).ToArray();
|
||||
File.WriteAllLines($"{checkFile}.txt", lines);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user