DeleteOldLogFilesAndDeleteEmptyDirectories
Log bug Wafer Counter trigger: - master
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
@ -77,7 +78,7 @@ internal static partial class HelperCreateNoteFiles
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void CleanExistingFiles(string directory, long ticks)
|
||||
private static void CleanExistingFiles(ILogger<Worker> logger, string directory, long ticks)
|
||||
{
|
||||
string check;
|
||||
string[] lines;
|
||||
@ -121,7 +122,7 @@ internal static partial class HelperCreateNoteFiles
|
||||
continue;
|
||||
File.Move(file, checkFile);
|
||||
}
|
||||
_ = HelperDeleteEmptyDirectories.DeleteEmptyDirectories(directory);
|
||||
HelperDeleteEmptyDirectories.DeleteEmptyDirectories(logger, directory);
|
||||
}
|
||||
|
||||
private static void CreateDailyNotes(string argsZero, long ticks)
|
||||
@ -272,11 +273,11 @@ internal static partial class HelperCreateNoteFiles
|
||||
}
|
||||
}
|
||||
|
||||
internal static void CreateNoteFiles(string argsZero)
|
||||
internal static void CreateNoteFiles(ILogger<Worker> logger, string argsZero)
|
||||
{
|
||||
long ticks = DateTime.Now.Ticks;
|
||||
List<string> importFiles = new();
|
||||
CleanExistingFiles(argsZero, ticks);
|
||||
CleanExistingFiles(logger, argsZero, ticks);
|
||||
importFiles.AddRange(Directory.GetFiles(argsZero, "*.csv", SearchOption.TopDirectoryOnly));
|
||||
importFiles.AddRange(Directory.GetFiles(argsZero, "*.tsv", SearchOption.TopDirectoryOnly));
|
||||
if (importFiles.Count == 0)
|
||||
|
@ -1,43 +1,86 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperDeleteEmptyDirectories
|
||||
{
|
||||
|
||||
internal static bool DeleteEmptyDirectories(string rootDirectory)
|
||||
private static void DeleteOldLogFilesAndDeleteEmptyDirectories(long? ticks, string? searchPattern, string checkDirectory, List<string> deletedDirectories)
|
||||
{
|
||||
bool result;
|
||||
if (!Directory.Exists(rootDirectory))
|
||||
result = false;
|
||||
else
|
||||
string[] files;
|
||||
FileInfo fileInfo;
|
||||
string[] directories = Directory.GetDirectories(checkDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
if (ticks is not null && !string.IsNullOrEmpty(searchPattern))
|
||||
{
|
||||
string[] files;
|
||||
string[] directories = Directory.GetDirectories(rootDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
if (directories.Length > 0)
|
||||
files = Array.Empty<string>();
|
||||
else
|
||||
files = Directory.GetFiles(rootDirectory, "*", SearchOption.AllDirectories);
|
||||
if (directories.Length == 0 && files.Length == 0)
|
||||
files = Directory.GetFiles(checkDirectory, searchPattern, SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
result = true;
|
||||
Directory.Delete(rootDirectory);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = false;
|
||||
foreach (string directory in directories)
|
||||
{
|
||||
result = DeleteEmptyDirectories(directory);
|
||||
if (result)
|
||||
result = DeleteEmptyDirectories(directory);
|
||||
}
|
||||
if (files is null)
|
||||
{
|
||||
directories = Directory.GetDirectories(rootDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
result = directories.Length == 0;
|
||||
}
|
||||
fileInfo = new(file);
|
||||
if (fileInfo.LastWriteTime.Ticks > ticks)
|
||||
continue;
|
||||
File.Delete(file);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
if (directories.Length > 0)
|
||||
files = Array.Empty<string>();
|
||||
else
|
||||
files = Directory.GetFiles(checkDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
if (directories.Length == 0 && files.Length == 0)
|
||||
{
|
||||
try
|
||||
{ Directory.Delete(checkDirectory); }
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
new DirectoryInfo(checkDirectory).Attributes = FileAttributes.Normal;
|
||||
Directory.Delete(checkDirectory);
|
||||
}
|
||||
deletedDirectories.Add(checkDirectory);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<string> check = new();
|
||||
foreach (string directory in directories)
|
||||
{
|
||||
DeleteOldLogFilesAndDeleteEmptyDirectories(ticks, searchPattern, directory, check);
|
||||
deletedDirectories.AddRange(check);
|
||||
if (check.Count > 0 && Directory.Exists(directory))
|
||||
DeleteOldLogFilesAndDeleteEmptyDirectories(ticks, searchPattern, directory, deletedDirectories);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void DeleteOldLogFilesAndDeleteEmptyDirectories(ILogger<Worker> logger, long? ticks, string? searchPattern, string rootDirectory)
|
||||
{
|
||||
List<string> check = new();
|
||||
List<string> directories = Directory.GetDirectories(rootDirectory, "*", SearchOption.TopDirectoryOnly).ToList();
|
||||
directories.Add(rootDirectory);
|
||||
foreach (string directory in directories)
|
||||
{
|
||||
logger.LogInformation("{directoryName}", Path.GetFileName(directory));
|
||||
for (int i = 1; i < 50; i++)
|
||||
{
|
||||
if (!Directory.Exists(directory))
|
||||
break;
|
||||
check.Clear();
|
||||
DeleteOldLogFilesAndDeleteEmptyDirectories(ticks, searchPattern, directory, check);
|
||||
if (check.Count == 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static void DeleteEmptyDirectories(ILogger<Worker> logger, string rootDirectory)
|
||||
{
|
||||
long? ticks = null;
|
||||
string? searchPattern = null;
|
||||
DeleteOldLogFilesAndDeleteEmptyDirectories(logger, ticks, searchPattern, rootDirectory);
|
||||
}
|
||||
|
||||
internal static void DeleteOldLogFilesAndDeleteEmptyDirectories(ILogger<Worker> logger, string rootDirectory)
|
||||
{
|
||||
long ticks = DateTime.Now.AddHours(-120).Ticks;
|
||||
DeleteOldLogFilesAndDeleteEmptyDirectories(logger, ticks, "*.log", rootDirectory);
|
||||
DeleteOldLogFilesAndDeleteEmptyDirectories(logger, ticks, "*.log.*", rootDirectory);
|
||||
}
|
||||
|
||||
}
|
@ -37,6 +37,8 @@ internal static class HelperHardcodedFileSearchAndSort
|
||||
"SP101",
|
||||
"SPV01",
|
||||
"SRP",
|
||||
"WC6Inch",
|
||||
"WC8Inch",
|
||||
"Bio-Rad"
|
||||
};
|
||||
string[] files = Directory.GetFiles(sourceDirectory, "*", searchOption);
|
||||
|
@ -1137,11 +1137,17 @@ internal static partial class HelperMarkdown
|
||||
{
|
||||
if (string.IsNullOrEmpty(input.StartAt) || string.IsNullOrEmpty(input.Destination))
|
||||
throw new NotSupportedException();
|
||||
ReadOnlyDictionary<string, List<Card>> columnsToCards = GetColumnsToCards(input, relativeToCollection);
|
||||
if (columnsToCards.Count > 0)
|
||||
ReadOnlyDictionary<string, List<Card>> columnsToCards;
|
||||
string jsonFile = Path.Combine(input.Destination, $"{nameof(columnsToCards)}.json");
|
||||
if (File.Exists(jsonFile))
|
||||
File.Delete(jsonFile);
|
||||
columnsToCards = GetColumnsToCards(input, relativeToCollection);
|
||||
if (columnsToCards.Count == 0)
|
||||
File.WriteAllText(jsonFile, "{}");
|
||||
else
|
||||
{
|
||||
string json = JsonSerializer.Serialize(columnsToCards, ColumnsAndCardsSourceGenerationContext.Default.ReadOnlyDictionaryStringListCard);
|
||||
File.WriteAllText(Path.Combine(input.Destination, $"{nameof(columnsToCards)}.json"), json);
|
||||
File.WriteAllText(jsonFile, json);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperTooLong
|
||||
@ -88,7 +90,7 @@ internal static class HelperTooLong
|
||||
}
|
||||
}
|
||||
|
||||
internal static void UpdateDateVerifyAndGetTicksDirectories(Models.AppSettings appSettings, string directory)
|
||||
internal static void UpdateDateVerifyAndGetTicksDirectories(ILogger<Worker> logger, Models.AppSettings appSettings, string directory)
|
||||
{
|
||||
string ticksDirectoryName;
|
||||
DirectoryInfo directoryInfo;
|
||||
@ -107,8 +109,7 @@ internal static class HelperTooLong
|
||||
Directory.SetLastWriteTime(ticksDirectory, new DateTime(directoryTicks));
|
||||
}
|
||||
MaybeMoveFiles(appSettings, ticksDirectories);
|
||||
for (int i = 1; i < 10; i++)
|
||||
_ = HelperDeleteEmptyDirectories.DeleteEmptyDirectories(directory);
|
||||
HelperDeleteEmptyDirectories.DeleteEmptyDirectories(logger, directory);
|
||||
}
|
||||
|
||||
}
|
@ -11,7 +11,7 @@ internal static partial class HelperZipFilesByDate
|
||||
[GeneratedRegex("[a-zA-Z0-9]{1,}")]
|
||||
private static partial Regex LowerAlphaAlphaAndNumber();
|
||||
|
||||
private static bool SetDateFromZipEntry(ILogger log, string[] zipFiles, string keyFile, string keyFileB, string keyFileC)
|
||||
private static bool SetDateFromZipEntry(ILogger<Worker> logger, string[] zipFiles, string keyFile, string keyFileB, string keyFileC)
|
||||
{
|
||||
bool result = false;
|
||||
string[] files;
|
||||
@ -81,7 +81,7 @@ internal static partial class HelperZipFilesByDate
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
log.LogInformation("<{zipFile}> is invalid!", zipFile);
|
||||
logger.LogInformation("<{zipFile}> is invalid!", zipFile);
|
||||
checkFile = string.Concat(zipFile, ".err");
|
||||
for (int e = 0; e < short.MaxValue; e++)
|
||||
{
|
||||
@ -91,12 +91,12 @@ internal static partial class HelperZipFilesByDate
|
||||
}
|
||||
try
|
||||
{ File.Move(zipFile, checkFile); }
|
||||
catch (Exception) { log.LogInformation("<{zipFile}> couldn't be moved!", zipFile); }
|
||||
catch (Exception) { logger.LogInformation("<{zipFile}> couldn't be moved!", zipFile); }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static bool ZipFilesByDate(ILogger log, string sourceDirectory, SearchOption searchOption = SearchOption.TopDirectoryOnly, string dayFormat = "")
|
||||
internal static bool ZipFilesByDate(ILogger<Worker> logger, string sourceDirectory, SearchOption searchOption = SearchOption.TopDirectoryOnly, string dayFormat = "")
|
||||
{
|
||||
bool result = false;
|
||||
string key;
|
||||
@ -230,17 +230,17 @@ internal static partial class HelperZipFilesByDate
|
||||
}
|
||||
if (topDirectory != sourceDirectory)
|
||||
try
|
||||
{ _ = HelperDeleteEmptyDirectories.DeleteEmptyDirectories(topDirectory); }
|
||||
{ HelperDeleteEmptyDirectories.DeleteEmptyDirectories(logger, topDirectory); }
|
||||
catch (Exception) { }
|
||||
log.LogInformation("{topDirectory}", topDirectory);
|
||||
logger.LogInformation("{topDirectory}", topDirectory);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static bool SetDateFromZipEntryForNuspec(ILogger log, string[] files) =>
|
||||
SetDateFromZipEntry(log, files, ".nuspec", "icon", "readme");
|
||||
internal static bool SetDateFromZipEntryForNuspec(ILogger<Worker> logger, string[] files) =>
|
||||
SetDateFromZipEntry(logger, files, ".nuspec", "icon", "readme");
|
||||
|
||||
internal static bool SetDateFromZipEntry(ILogger log, string sourceDirectory, SearchOption searchOption = SearchOption.AllDirectories)
|
||||
internal static bool SetDateFromZipEntry(ILogger<Worker> logger, string sourceDirectory, SearchOption searchOption = SearchOption.AllDirectories)
|
||||
{
|
||||
bool result = false;
|
||||
bool loop;
|
||||
@ -260,7 +260,7 @@ internal static partial class HelperZipFilesByDate
|
||||
_ => throw new NotSupportedException()
|
||||
};
|
||||
zipFiles = Directory.GetFiles(sourceDirectory, searchPattern, searchOption);
|
||||
loop = SetDateFromZipEntry(log, zipFiles, keyFile, keyFileB, keyFileC);
|
||||
loop = SetDateFromZipEntry(logger, zipFiles, keyFile, keyFileB, keyFileC);
|
||||
if (loop && !result)
|
||||
result = true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user