MoveToDelete
This commit is contained in:
parent
fe524b7536
commit
7f1f149eac
@ -161,47 +161,6 @@ internal static partial class Helper20241224
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ReadOnlyDictionary<string, List<FileInfo>> GetKeyValuePairs(string directory, string searchPattern, string split)
|
|
||||||
{
|
|
||||||
string key;
|
|
||||||
List<FileInfo>? collection;
|
|
||||||
Dictionary<string, List<FileInfo>> results = [];
|
|
||||||
string[] files = Directory.GetFiles(directory, searchPattern, SearchOption.TopDirectoryOnly);
|
|
||||||
FileInfo[] fileInfoCollection = files.Select(l => new FileInfo(l)).ToArray();
|
|
||||||
foreach (FileInfo fileInfo in fileInfoCollection.OrderBy(l => l.LastWriteTime))
|
|
||||||
{
|
|
||||||
key = fileInfo.Name.Split(split)[0];
|
|
||||||
if (!results.TryGetValue(key, out collection))
|
|
||||||
{
|
|
||||||
results.Add(key, []);
|
|
||||||
if (!results.TryGetValue(key, out collection))
|
|
||||||
throw new Exception();
|
|
||||||
}
|
|
||||||
collection.Add(fileInfo);
|
|
||||||
}
|
|
||||||
return results.AsReadOnly();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void DeleteFirst(string directory)
|
|
||||||
{
|
|
||||||
string checkFile;
|
|
||||||
FileInfo fileInfo;
|
|
||||||
ReadOnlyDictionary<string, List<FileInfo>> keyValuePairs = GetKeyValuePairs(directory, "*.tar", $"-{DateTime.Now.Year}-");
|
|
||||||
foreach (KeyValuePair<string, List<FileInfo>> keyValuePair in keyValuePairs)
|
|
||||||
{
|
|
||||||
if (keyValuePair.Value.Count < 3)
|
|
||||||
continue;
|
|
||||||
for (int i = 1; i < keyValuePair.Value.Count - 1; i++)
|
|
||||||
{
|
|
||||||
fileInfo = keyValuePair.Value[i];
|
|
||||||
checkFile = Path.Combine($"{fileInfo.Directory}-Delete", fileInfo.Name);
|
|
||||||
if (File.Exists(checkFile))
|
|
||||||
continue;
|
|
||||||
File.Move(fileInfo.FullName, checkFile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static void Compare(ILogger<Worker> logger, List<string> args)
|
internal static void Compare(ILogger<Worker> logger, List<string> args)
|
||||||
{
|
{
|
||||||
string host = args[2];
|
string host = args[2];
|
||||||
@ -209,7 +168,6 @@ internal static partial class Helper20241224
|
|||||||
string format = NginxFileSystem.GetFormat();
|
string format = NginxFileSystem.GetFormat();
|
||||||
TimeZoneInfo timeZoneInfo = TimeZoneInfo.Local;
|
TimeZoneInfo timeZoneInfo = TimeZoneInfo.Local;
|
||||||
string compareDirectory = Path.GetFullPath(args[0]);
|
string compareDirectory = Path.GetFullPath(args[0]);
|
||||||
DeleteFirst(compareDirectory);
|
|
||||||
logger.LogInformation("Comparing files on {host}", host);
|
logger.LogInformation("Comparing files on {host}", host);
|
||||||
ReadOnlyCollection<Record> records = GetRecord(format, timeZoneInfo, host, new([rootDirectoryName]), compareDirectory);
|
ReadOnlyCollection<Record> records = GetRecord(format, timeZoneInfo, host, new([rootDirectoryName]), compareDirectory);
|
||||||
#if ShellProgressBar
|
#if ShellProgressBar
|
||||||
|
61
ADO2025/PI4/Helper-2025-01-01.cs
Normal file
61
ADO2025/PI4/Helper-2025-01-01.cs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
|
||||||
|
namespace File_Folder_Helper.ADO2025.PI4;
|
||||||
|
|
||||||
|
internal static partial class Helper20250101
|
||||||
|
{
|
||||||
|
|
||||||
|
private static ReadOnlyDictionary<string, List<FileInfo>> GetKeyValuePairs(string directory, string searchPattern, string split)
|
||||||
|
{
|
||||||
|
string key;
|
||||||
|
List<FileInfo>? collection;
|
||||||
|
Dictionary<string, List<FileInfo>> results = [];
|
||||||
|
string[] files = Directory.GetFiles(directory, searchPattern, SearchOption.TopDirectoryOnly);
|
||||||
|
FileInfo[] fileInfoCollection = files.Select(l => new FileInfo(l)).ToArray();
|
||||||
|
foreach (FileInfo fileInfo in fileInfoCollection.OrderBy(l => l.LastWriteTime))
|
||||||
|
{
|
||||||
|
key = fileInfo.Name.Split(split)[0];
|
||||||
|
if (!results.TryGetValue(key, out collection))
|
||||||
|
{
|
||||||
|
results.Add(key, []);
|
||||||
|
if (!results.TryGetValue(key, out collection))
|
||||||
|
throw new Exception();
|
||||||
|
}
|
||||||
|
collection.Add(fileInfo);
|
||||||
|
}
|
||||||
|
return results.AsReadOnly();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void MoveToDelete(ILogger<Worker> logger, string appendage, ReadOnlyDictionary<string, List<FileInfo>> keyValuePairs)
|
||||||
|
{
|
||||||
|
string checkFile;
|
||||||
|
FileInfo fileInfo;
|
||||||
|
foreach (KeyValuePair<string, List<FileInfo>> keyValuePair in keyValuePairs)
|
||||||
|
{
|
||||||
|
if (keyValuePair.Value.Count < 3)
|
||||||
|
continue;
|
||||||
|
for (int i = 1; i < keyValuePair.Value.Count - 1; i++)
|
||||||
|
{
|
||||||
|
fileInfo = keyValuePair.Value[i];
|
||||||
|
checkFile = Path.Combine($"{fileInfo.Directory}{appendage}", fileInfo.Name);
|
||||||
|
if (File.Exists(checkFile))
|
||||||
|
continue;
|
||||||
|
logger.LogInformation("Moving <{fileInfo.FullName}> to <{checkFile}>", fileInfo.FullName, checkFile);
|
||||||
|
File.Move(fileInfo.FullName, checkFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void MoveToDelete(ILogger<Worker> logger, List<string> args)
|
||||||
|
{
|
||||||
|
string split = args[3];
|
||||||
|
string appendage = args[4];
|
||||||
|
string searchPattern = args[2];
|
||||||
|
string compareDirectory = Path.GetFullPath(args[0]);
|
||||||
|
ReadOnlyDictionary<string, List<FileInfo>> keyValuePairs = GetKeyValuePairs(compareDirectory, searchPattern, split);
|
||||||
|
logger.LogInformation("KeyValuePairs: {keyValuePairs}", keyValuePairs.Count);
|
||||||
|
MoveToDelete(logger, appendage, keyValuePairs);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -125,6 +125,8 @@ internal static class HelperDay
|
|||||||
ADO2024.PI4.Helper20241217.Backup(logger, args);
|
ADO2024.PI4.Helper20241217.Backup(logger, args);
|
||||||
else if (args[1] == "Day-Helper-2024-12-24")
|
else if (args[1] == "Day-Helper-2024-12-24")
|
||||||
ADO2024.PI4.Helper20241224.Compare(logger, args);
|
ADO2024.PI4.Helper20241224.Compare(logger, args);
|
||||||
|
else if (args[1] == "Day-Helper-2025-01-01")
|
||||||
|
ADO2025.PI4.Helper20250101.MoveToDelete(logger, args);
|
||||||
else
|
else
|
||||||
throw new Exception(appSettings.Company);
|
throw new Exception(appSettings.Company);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user