Ready to test
This commit is contained in:
43
Helpers/HelperDeleteEmptyDirectories.cs
Normal file
43
Helpers/HelperDeleteEmptyDirectories.cs
Normal file
@ -0,0 +1,43 @@
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperDeleteEmptyDirectories
|
||||
{
|
||||
|
||||
internal static bool DeleteEmptyDirectories(string rootDirectory)
|
||||
{
|
||||
bool result;
|
||||
if (!Directory.Exists(rootDirectory))
|
||||
result = false;
|
||||
else
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
44
Helpers/HelperILMerge.cs
Normal file
44
Helpers/HelperILMerge.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperILMerge
|
||||
{
|
||||
|
||||
internal static void ILMerge(string workingDirectory)
|
||||
{
|
||||
bool result;
|
||||
ProcessStartInfo processStartInfo;
|
||||
StringBuilder stringBuilder = new();
|
||||
string[] successChecks = new string[] { "success" };
|
||||
string[] errorChecks = new string[] { "Error", "Conflict", "error:" };
|
||||
string errorFile = Path.Combine(workingDirectory, string.Concat(Path.GetFileName(workingDirectory), ".err"));
|
||||
string primaryFile = Path.Combine(workingDirectory, string.Concat(Path.GetFileName(workingDirectory), ".dll"));
|
||||
string[] dllFiles = Directory.GetFiles(workingDirectory, "*.dll", SearchOption.TopDirectoryOnly);
|
||||
FileInfo ilMerge = new(@"C:\Users\phares\AppData\Local\IFXApps\ILMerge\ILMerge.exe");
|
||||
FileInfo fileInfo = new(Path.Combine(workingDirectory, ilMerge.Name));
|
||||
if (!fileInfo.Exists)
|
||||
_ = ilMerge.CopyTo(fileInfo.FullName);
|
||||
if (fileInfo.Exists && ilMerge.LastWriteTime != fileInfo.LastWriteTime)
|
||||
_ = ilMerge.CopyTo(fileInfo.FullName, overwrite: true);
|
||||
_ = stringBuilder.Append("/allowDup /target:library /out:\"").Append(Path.GetFileNameWithoutExtension(primaryFile)).Append(".all.dll\" ");
|
||||
foreach (string dllFile in dllFiles)
|
||||
{
|
||||
if (dllFile == primaryFile)
|
||||
continue;
|
||||
_ = stringBuilder.Append('"').Append(Path.GetFileName(dllFile)).Append("\" ");
|
||||
}
|
||||
processStartInfo = new ProcessStartInfo(fileInfo.FullName, stringBuilder.ToString())
|
||||
{
|
||||
UseShellExecute = false,
|
||||
RedirectStandardError = true,
|
||||
RedirectStandardOutput = true,
|
||||
WorkingDirectory = workingDirectory,
|
||||
};
|
||||
result = HelperStart.Start(errorChecks, successChecks, processStartInfo, errorFile);
|
||||
if (result)
|
||||
{ }
|
||||
}
|
||||
|
||||
}
|
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)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
50
Helpers/HelperRenameToOldMoveDeleteOld.cs
Normal file
50
Helpers/HelperRenameToOldMoveDeleteOld.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using Serilog;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperRenameToOldMoveDeleteOldMerge
|
||||
{
|
||||
|
||||
internal static void RenameToOldMoveDeleteOld(ILogger log, string argsZero)
|
||||
{
|
||||
string checkDirectory = argsZero[0..^1];
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
log.Information(string.Concat("<", checkDirectory, "> doesn't exist!"));
|
||||
else
|
||||
{
|
||||
string renameFile;
|
||||
string destinationFile;
|
||||
List<string> deleteFiles = new();
|
||||
string[] moveFiles = Directory.GetFiles(argsZero, "*", SearchOption.TopDirectoryOnly);
|
||||
log.Information(string.Concat("<", moveFiles.Length, "> to move"));
|
||||
foreach (string moveFile in moveFiles)
|
||||
{
|
||||
destinationFile = string.Concat(checkDirectory, moveFile[argsZero.Length..]);
|
||||
if (!File.Exists(destinationFile))
|
||||
File.Move(moveFile, destinationFile);
|
||||
else
|
||||
{
|
||||
renameFile = Path.ChangeExtension(destinationFile, string.Concat(Path.GetExtension(destinationFile), ".old"));
|
||||
File.Move(destinationFile, renameFile);
|
||||
File.Move(moveFile, destinationFile);
|
||||
deleteFiles.Add(renameFile);
|
||||
}
|
||||
}
|
||||
log.Information(string.Concat("<", deleteFiles.Count, "> to delete"));
|
||||
foreach (string deleteFile in deleteFiles)
|
||||
{
|
||||
for (short i = 0; i < short.MaxValue; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(deleteFile);
|
||||
break;
|
||||
}
|
||||
catch (Exception) { }
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
182
Helpers/HelperSaveOrCopyContents.cs
Normal file
182
Helpers/HelperSaveOrCopyContents.cs
Normal file
@ -0,0 +1,182 @@
|
||||
using Serilog;
|
||||
using System.Text;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperSaveOrCopyContents
|
||||
{
|
||||
|
||||
internal static void SaveOrCopyContents(ILogger log, string argsZero, ConsoleKey consoleKey)
|
||||
{
|
||||
SearchOption searchOption;
|
||||
bool fileNameWithoutExtension;
|
||||
bool alongSideTopDirectoryOnly = false;
|
||||
bool alongSideAllDirectories = false;
|
||||
bool clipboardTopDirectoryOnly = false;
|
||||
switch (consoleKey)
|
||||
{
|
||||
case ConsoleKey.A:
|
||||
alongSideTopDirectoryOnly = true;
|
||||
searchOption = SearchOption.TopDirectoryOnly;
|
||||
fileNameWithoutExtension = false;
|
||||
break;
|
||||
case ConsoleKey.B:
|
||||
alongSideAllDirectories = true;
|
||||
searchOption = SearchOption.AllDirectories;
|
||||
fileNameWithoutExtension = false;
|
||||
break;
|
||||
case ConsoleKey.C:
|
||||
clipboardTopDirectoryOnly = true;
|
||||
searchOption = SearchOption.TopDirectoryOnly;
|
||||
fileNameWithoutExtension = false;
|
||||
break;
|
||||
case ConsoleKey.D:
|
||||
clipboardTopDirectoryOnly = true;
|
||||
searchOption = SearchOption.AllDirectories;
|
||||
fileNameWithoutExtension = false;
|
||||
break;
|
||||
case ConsoleKey.E:
|
||||
clipboardTopDirectoryOnly = true;
|
||||
searchOption = SearchOption.TopDirectoryOnly;
|
||||
fileNameWithoutExtension = true;
|
||||
break;
|
||||
case ConsoleKey.F:
|
||||
clipboardTopDirectoryOnly = true;
|
||||
searchOption = SearchOption.AllDirectories;
|
||||
fileNameWithoutExtension = true;
|
||||
break;
|
||||
default:
|
||||
throw new Exception();
|
||||
}
|
||||
log.Information("D) Directory, F) File or B) Both?");
|
||||
ConsoleKey dfb = Console.ReadKey().Key;
|
||||
log.Information(string.Empty);
|
||||
if (dfb is not ConsoleKey.D and not ConsoleKey.F and not ConsoleKey.B)
|
||||
throw new Exception("Not valid");
|
||||
else
|
||||
{
|
||||
string fileName = dfb switch
|
||||
{
|
||||
ConsoleKey.D => "Directories",
|
||||
ConsoleKey.F => "Files",
|
||||
ConsoleKey.B => "Both",
|
||||
_ => throw new Exception(),
|
||||
};
|
||||
long now = DateTime.Now.Ticks;
|
||||
StringBuilder data = new();
|
||||
string[] dataCollection;
|
||||
string filePathAndName = string.Concat(Path.GetDirectoryName(argsZero), @"\", fileName, ".txt");
|
||||
if (alongSideTopDirectoryOnly)
|
||||
File.WriteAllText(filePathAndName, string.Empty);
|
||||
else if (alongSideAllDirectories)
|
||||
File.WriteAllText(filePathAndName, string.Concat(argsZero, Environment.NewLine, "Start", Environment.NewLine));
|
||||
switch (dfb)
|
||||
{
|
||||
case ConsoleKey.D:
|
||||
if (alongSideTopDirectoryOnly || clipboardTopDirectoryOnly)
|
||||
{
|
||||
dataCollection = Directory.GetDirectories(argsZero, "*", searchOption);
|
||||
if (!(dataCollection == null) && dataCollection.Length > 0)
|
||||
{
|
||||
foreach (string s in dataCollection)
|
||||
{
|
||||
if (fileNameWithoutExtension)
|
||||
_ = data.Append('\'').Append(Path.GetFileNameWithoutExtension(s)).Append("', ");
|
||||
else
|
||||
_ = data.AppendLine(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (alongSideAllDirectories)
|
||||
{
|
||||
dataCollection = Directory.GetDirectories(argsZero, "*", searchOption);
|
||||
if (!(dataCollection == null) && dataCollection.Length > 0)
|
||||
{
|
||||
foreach (string s in dataCollection)
|
||||
File.AppendAllText(filePathAndName, string.Concat(s.Replace(argsZero, "../"), Environment.NewLine));
|
||||
}
|
||||
}
|
||||
else
|
||||
throw new Exception();
|
||||
break;
|
||||
case ConsoleKey.F:
|
||||
if (alongSideTopDirectoryOnly || clipboardTopDirectoryOnly)
|
||||
{
|
||||
dataCollection = Directory.GetFiles(argsZero, "*", searchOption);
|
||||
if (!(dataCollection == null) && dataCollection.Length > 0)
|
||||
{
|
||||
foreach (string s in dataCollection)
|
||||
{
|
||||
if (fileNameWithoutExtension)
|
||||
_ = data.Append('\'').Append(Path.GetFileNameWithoutExtension(s)).Append("', ");
|
||||
else
|
||||
_ = data.AppendLine(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (alongSideAllDirectories)
|
||||
{
|
||||
dataCollection = Directory.GetFiles(argsZero, "*", searchOption);
|
||||
if (!(dataCollection == null) && dataCollection.Length > 0)
|
||||
{
|
||||
foreach (string s in dataCollection)
|
||||
File.AppendAllText(filePathAndName, string.Concat(s.Replace(argsZero, "../"), Environment.NewLine));
|
||||
}
|
||||
}
|
||||
else
|
||||
throw new Exception();
|
||||
break;
|
||||
case ConsoleKey.B:
|
||||
if (alongSideTopDirectoryOnly || clipboardTopDirectoryOnly)
|
||||
{
|
||||
dataCollection = Directory.GetFiles(argsZero, "*", searchOption);
|
||||
if (!(dataCollection == null) && dataCollection.Length > 0)
|
||||
{
|
||||
foreach (string s in dataCollection)
|
||||
{
|
||||
if (fileNameWithoutExtension)
|
||||
_ = data.Append('\'').Append(Path.GetFileNameWithoutExtension(s)).Append("', ");
|
||||
else
|
||||
_ = data.AppendLine(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (alongSideAllDirectories)
|
||||
{
|
||||
dataCollection = Directory.GetDirectories(argsZero, "*", searchOption);
|
||||
if (!(dataCollection == null) && dataCollection.Length > 0)
|
||||
{
|
||||
foreach (string s in dataCollection)
|
||||
File.AppendAllText(filePathAndName, string.Concat(s.Replace(argsZero, "../"), Environment.NewLine));
|
||||
}
|
||||
}
|
||||
else
|
||||
throw new Exception();
|
||||
break;
|
||||
default:
|
||||
throw new Exception();
|
||||
}
|
||||
TimeSpan timeSpan = new(DateTime.Now.Ticks - now);
|
||||
log.Information(string.Concat(timeSpan.TotalSeconds, " TotalSeconds"));
|
||||
if (alongSideTopDirectoryOnly)
|
||||
{
|
||||
File.WriteAllText(filePathAndName, data.ToString());
|
||||
log.Information("Data written");
|
||||
}
|
||||
else if (clipboardTopDirectoryOnly)
|
||||
{
|
||||
TextCopy.ClipboardService.SetText(data.ToString());
|
||||
log.Information("Data stored in clipboard");
|
||||
}
|
||||
else if (alongSideAllDirectories)
|
||||
{
|
||||
File.AppendAllText(filePathAndName, "Done");
|
||||
log.Information("Data written");
|
||||
}
|
||||
else
|
||||
throw new Exception();
|
||||
log.Information("Press any key to close");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
55
Helpers/HelperStart.cs
Normal file
55
Helpers/HelperStart.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperStart
|
||||
{
|
||||
|
||||
internal static bool Start(string[] errorChecks, string[] successChecks, ProcessStartInfo processStartInfo, string errorFile)
|
||||
{
|
||||
bool result = false;
|
||||
string standardError = string.Empty;
|
||||
string standardOutput = string.Empty;
|
||||
StringBuilder stringBuilder = new();
|
||||
Process? process = Process.Start(processStartInfo);
|
||||
long breakAfter = DateTime.Now.AddSeconds(20).Ticks;
|
||||
for (short j = 0; j < short.MaxValue; j++)
|
||||
{
|
||||
if (process is null || process.HasExited || process.WaitForExit(500) || DateTime.Now.Ticks > breakAfter)
|
||||
break;
|
||||
}
|
||||
if (process is not null && processStartInfo.RedirectStandardOutput)
|
||||
{
|
||||
_ = stringBuilder.Append(process.StandardOutput.ReadToEnd());
|
||||
standardOutput = stringBuilder.ToString();
|
||||
if (string.IsNullOrEmpty(standardOutput))
|
||||
standardOutput = string.Concat(standardOutput);
|
||||
if (!string.IsNullOrEmpty(standardOutput))
|
||||
{
|
||||
result = (from l in successChecks where standardOutput.Contains(l) select 1).Any();
|
||||
if (result)
|
||||
result = !(from l in errorChecks where standardOutput.Contains(l) select 1).Any();
|
||||
}
|
||||
}
|
||||
if (process is not null && processStartInfo.RedirectStandardError)
|
||||
{
|
||||
standardError = process.StandardError.ReadToEnd();
|
||||
if (!string.IsNullOrEmpty(standardError))
|
||||
{
|
||||
if (result)
|
||||
{
|
||||
result = (from l in successChecks where standardError.Contains(l) select 1).Any();
|
||||
if (result)
|
||||
result = !(from l in errorChecks where standardError.Contains(l) select 1).Any();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!result)
|
||||
result = standardOutput == string.Empty && standardError == string.Empty;
|
||||
if (!result)
|
||||
File.WriteAllLines(errorFile, new string[] { standardOutput, Environment.NewLine, Environment.NewLine, standardError });
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
32
Helpers/HelperTooLong.cs
Normal file
32
Helpers/HelperTooLong.cs
Normal file
@ -0,0 +1,32 @@
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperTooLong
|
||||
{
|
||||
|
||||
internal static void TooLong(string workingDirectory, bool delete)
|
||||
{
|
||||
string[] files;
|
||||
string destination;
|
||||
string? parentDirectory;
|
||||
string[] directories = Directory.GetDirectories(workingDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
for (int i = 0; i < directories.Length; i++)
|
||||
{
|
||||
if (delete)
|
||||
{
|
||||
Directory.Delete(directories[i], recursive: true);
|
||||
break;
|
||||
}
|
||||
files = Directory.GetFiles(directories[i], "*", SearchOption.TopDirectoryOnly);
|
||||
for (int f = 0; f < files.Length; f++)
|
||||
File.Delete(files[f]);
|
||||
parentDirectory = Path.GetDirectoryName(directories[i]);
|
||||
if (string.IsNullOrEmpty(parentDirectory))
|
||||
continue;
|
||||
destination = Path.Combine(parentDirectory, i.ToString());
|
||||
if (Path.GetFileName(directories[i]).Length > 3)
|
||||
Directory.Move(directories[i], destination);
|
||||
TooLong(destination, delete);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
139
Helpers/HelperZipFilesByDate.cs
Normal file
139
Helpers/HelperZipFilesByDate.cs
Normal file
@ -0,0 +1,139 @@
|
||||
using Serilog;
|
||||
using System.Globalization;
|
||||
using System.IO.Compression;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperZipFilesByDate
|
||||
{
|
||||
|
||||
internal static void ZipFilesByDate(ILogger log, string sourceDirectory, SearchOption searchOption = SearchOption.TopDirectoryOnly, string dayFormat = "")
|
||||
{
|
||||
string key;
|
||||
bool addFile;
|
||||
string fileName;
|
||||
string[] segments;
|
||||
string[] subFiles;
|
||||
string weekOfYear;
|
||||
FileInfo fileInfo;
|
||||
DateTime creationTime;
|
||||
DateTime lastWriteTime;
|
||||
Regex regex = new("[a-zA-Z0-9]{1,}");
|
||||
DateTime dateTime = DateTime.Now.AddDays(-6);
|
||||
DateTime firstEmail = new(2019, 3, 8);
|
||||
CultureInfo cultureInfo = new("en-US");
|
||||
Calendar calendar = cultureInfo.Calendar;
|
||||
int ticksLength = dateTime.Ticks.ToString().Length;
|
||||
Dictionary<string, DateTime> weeks = new();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
dateTime = firstEmail.AddDays(i);
|
||||
weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
||||
key = string.Concat(dateTime.ToString("yyyy"), "_Week_", weekOfYear);
|
||||
if (!weeks.ContainsKey(key))
|
||||
weeks.Add(key, dateTime);
|
||||
}
|
||||
weekOfYear = calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
||||
string skipKey = string.Concat(DateTime.Now.ToString("yyyy"), "_Week_", weekOfYear);
|
||||
Dictionary<string, List<string>> keyValuePairs = new();
|
||||
string[] topDirectories = Directory.GetDirectories(sourceDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
if (topDirectories.Length == 0)
|
||||
topDirectories = new string[] { sourceDirectory };
|
||||
foreach (string topDirectory in topDirectories)
|
||||
{
|
||||
keyValuePairs.Clear();
|
||||
subFiles = Directory.GetFiles(topDirectory, "*", searchOption);
|
||||
foreach (string subFile in subFiles)
|
||||
{
|
||||
addFile = false;
|
||||
if (subFile.EndsWith(".zip"))
|
||||
continue;
|
||||
fileName = Path.GetFileName(subFile);
|
||||
fileInfo = new FileInfo(subFile);
|
||||
creationTime = fileInfo.CreationTime;
|
||||
if (creationTime > dateTime)
|
||||
continue;
|
||||
lastWriteTime = fileInfo.LastWriteTime;
|
||||
if (fileName.Contains(lastWriteTime.ToString("yyyyMMdd")) || fileName.Contains(lastWriteTime.ToString("yyyy-MM-dd")) ||
|
||||
fileName.Contains(creationTime.ToString("yyyyMMdd")) || fileName.Contains(creationTime.ToString("yyyy-MM-dd")) ||
|
||||
fileName.Contains(lastWriteTime.ToString("yyMMdd")) || fileName.Contains(lastWriteTime.ToString("yy-MM-dd")) ||
|
||||
fileName.Contains(creationTime.ToString("yyMMdd")) || fileName.Contains(creationTime.ToString("yy-MM-dd")) ||
|
||||
fileName.Contains(lastWriteTime.AddDays(-1).ToString("yyyyMMdd")) || fileName.Contains(lastWriteTime.AddDays(-1).ToString("yyyy-MM-dd")) ||
|
||||
fileName.Contains(creationTime.AddDays(-1).ToString("yyyyMMdd")) || fileName.Contains(creationTime.AddDays(-1).ToString("yyyy-MM-dd")) ||
|
||||
fileName.Contains(lastWriteTime.AddDays(-1).ToString("yyMMdd")) || fileName.Contains(lastWriteTime.AddDays(-1).ToString("yy-MM-dd")) ||
|
||||
fileName.Contains(creationTime.AddDays(-1).ToString("yyMMdd")) || fileName.Contains(creationTime.AddDays(-1).ToString("yy-MM-dd")))
|
||||
addFile = true;
|
||||
if (!addFile && fileName.Length > ticksLength)
|
||||
{
|
||||
MatchCollection matches = regex.Matches(fileName);
|
||||
foreach (Match match in matches.Cast<Match>())
|
||||
{
|
||||
if (match.Value.Length != ticksLength)
|
||||
continue;
|
||||
if (!long.TryParse(match.Value, out long ticks))
|
||||
continue;
|
||||
addFile = true;
|
||||
break;
|
||||
}
|
||||
if (addFile)
|
||||
break;
|
||||
}
|
||||
if (addFile)
|
||||
{
|
||||
weekOfYear = calendar.GetWeekOfYear(lastWriteTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
||||
if (string.IsNullOrEmpty(dayFormat))
|
||||
key = string.Concat(lastWriteTime.ToString("yyyy"), "_Week_", weekOfYear);
|
||||
else
|
||||
key = string.Concat(lastWriteTime.ToString("yyyy"), "_Week_", weekOfYear, "_", lastWriteTime.ToString(dayFormat));
|
||||
if (key == skipKey)
|
||||
continue;
|
||||
if (!keyValuePairs.ContainsKey(key))
|
||||
keyValuePairs.Add(key, new List<string>());
|
||||
keyValuePairs[key].Add(subFile);
|
||||
}
|
||||
}
|
||||
foreach (KeyValuePair<string, List<string>> element in keyValuePairs)
|
||||
{
|
||||
key = string.Concat(topDirectory, @"\", element.Key, ".zip");
|
||||
if (File.Exists(key))
|
||||
{
|
||||
for (short i = 101; i < short.MaxValue; i++)
|
||||
{
|
||||
key = string.Concat(topDirectory, @"\", element.Key, "_", i, ".zip");
|
||||
if (!File.Exists(key))
|
||||
break;
|
||||
}
|
||||
}
|
||||
using ZipArchive zip = ZipFile.Open(key, ZipArchiveMode.Create);
|
||||
foreach (string file in element.Value)
|
||||
{
|
||||
_ = zip.CreateEntryFromFile(file, Path.GetFileName(file));
|
||||
File.Delete(file);
|
||||
}
|
||||
}
|
||||
subFiles = Directory.GetFiles(topDirectory, "*.zip", SearchOption.TopDirectoryOnly);
|
||||
foreach (string subFile in subFiles)
|
||||
{
|
||||
fileName = Path.GetFileNameWithoutExtension(subFile);
|
||||
segments = fileName.Split('_');
|
||||
if (segments.Length > 2)
|
||||
fileName = string.Concat(segments[0], '_', segments[1], '_', segments[2]);
|
||||
if (weeks.ContainsKey(fileName))
|
||||
{
|
||||
try
|
||||
{ File.SetLastWriteTime(subFile, weeks[fileName]); }
|
||||
catch (Exception) { }
|
||||
}
|
||||
}
|
||||
if (topDirectory != sourceDirectory)
|
||||
{
|
||||
try
|
||||
{ _ = HelperDeleteEmptyDirectories.DeleteEmptyDirectories(topDirectory); }
|
||||
catch (Exception) { }
|
||||
}
|
||||
log.Information(topDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user