log pattern
This commit is contained in:
2023-08-06 23:00:38 -07:00
parent 79b9daedfa
commit e6a70130c7
13 changed files with 756 additions and 455 deletions

View File

@ -5,30 +5,33 @@ using System.Text.RegularExpressions;
namespace File_Folder_Helper.Helpers;
internal static class HelperZipFilesByDate
internal static partial class HelperZipFilesByDate
{
[GeneratedRegex("[a-zA-Z0-9]{1,}")]
private static partial Regex LowerAlphaAlphaAndNumber();
internal static bool ZipFilesByDate(ILogger log, string sourceDirectory, SearchOption searchOption = SearchOption.TopDirectoryOnly, string dayFormat = "")
{
bool result = false;
string key;
bool addFile;
string? zipPath;
string fileName;
string? zipPath;
FileInfo fileInfo;
string weekOfYear;
string[] segments;
string[] subFiles;
string weekOfYear;
FileInfo fileInfo;
string zipDirectory;
DateTime creationTime;
string? directoryName;
DateTime lastWriteTime;
DateTime nowDateTime = DateTime.Now;
Regex regex = new("[a-zA-Z0-9]{1,}");
DateTime dateTime = DateTime.MinValue;
DateTime firstEmail = new(2019, 3, 8);
CultureInfo cultureInfo = new("en-US");
Calendar calendar = cultureInfo.Calendar;
Regex regex = LowerAlphaAlphaAndNumber();
Dictionary<string, DateTime> weeks = new();
int ticksLength = nowDateTime.AddDays(-6).Ticks.ToString().Length;
for (int i = 0; i < int.MaxValue; i++)
@ -107,14 +110,12 @@ internal static class HelperZipFilesByDate
{
key = Path.Combine(zipDirectory, $"{element.Key}.zip");
if (File.Exists(key))
{
for (short i = 101; i < short.MaxValue; i++)
{
key = Path.Combine(zipDirectory, $"{element.Key}_{i}.zip");
if (!File.Exists(key))
break;
}
}
using ZipArchive zip = ZipFile.Open(key, ZipArchiveMode.Create);
foreach (string file in element.Value)
{
@ -122,11 +123,9 @@ internal static class HelperZipFilesByDate
File.Delete(file);
}
if (zipPath is not null && Directory.Exists(zipPath) && !string.IsNullOrEmpty(directoryName))
{
try
{ Directory.SetLastWriteTime(directoryName, DateTime.Now); }
catch (Exception) { }
}
}
subFiles = Directory.GetFiles(zipDirectory, "*.zip", SearchOption.TopDirectoryOnly);
foreach (string subFile in subFiles)
@ -136,7 +135,6 @@ internal static class HelperZipFilesByDate
if (segments.Length > 2)
fileName = string.Concat(segments[0], '_', segments[1], '_', segments[2]);
if (weeks.TryGetValue(fileName, out DateTime value))
{
try
{
if (!result)
@ -144,14 +142,11 @@ internal static class HelperZipFilesByDate
File.SetLastWriteTime(subFile, value);
}
catch (Exception) { }
}
}
if (topDirectory != sourceDirectory)
{
try
{ _ = HelperDeleteEmptyDirectories.DeleteEmptyDirectories(topDirectory); }
catch (Exception) { }
}
log.LogInformation("{topDirectory}", topDirectory);
}
return result;
@ -166,7 +161,6 @@ internal static class HelperZipFilesByDate
string? zipDirectory;
DateTimeOffset? dateTimeOffset;
foreach (string zipFile in zipFiles)
{
try
{
dateTimeOffset = null;
@ -240,7 +234,6 @@ internal static class HelperZipFilesByDate
{ File.Move(zipFile, checkFile); }
catch (Exception) { log.LogInformation("<{zipFile}> couldn't be moved!", zipFile); }
}
}
return result;
}