Move Bank PDF files to Year Month
This commit is contained in:
93
ADO2025/PI4/Helper-2025-01-26.cs
Normal file
93
ADO2025/PI4/Helper-2025-01-26.cs
Normal file
@ -0,0 +1,93 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
|
||||
namespace File_Folder_Helper.ADO2025.PI4;
|
||||
|
||||
internal static partial class Helper20250126
|
||||
{
|
||||
|
||||
private static void Move(string file, string fileName, string checkFile, List<string> foundLines, ReadOnlyCollection<DateTime> dateTimes)
|
||||
{
|
||||
string checkDirectory = Path.Combine(Path.GetDirectoryName(file) ?? throw new Exception(), dateTimes[0].ToString("yyyy-MM"));
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
string fileNameB = Path.GetFileName(checkFile);
|
||||
string checkFileB = Path.Combine(checkDirectory, fileName);
|
||||
string checkFileC = Path.Combine(checkDirectory, fileNameB);
|
||||
string contents = string.Join(Environment.NewLine, foundLines);
|
||||
string checkFileD = Path.Combine(checkDirectory, $"{fileName}.txt");
|
||||
if (!File.Exists(checkFileB))
|
||||
File.Move(file, checkFileB);
|
||||
if (!File.Exists(checkFileC))
|
||||
File.Move(checkFile, checkFileC);
|
||||
File.WriteAllText(checkFileD, contents);
|
||||
}
|
||||
|
||||
private static void Move(ILogger<Worker> logger, string dateFormat, string file, string checkFile, string fileName, ReadOnlyCollection<string> statementPeriodSegments, List<string> foundLines)
|
||||
{
|
||||
DateTime dateTime;
|
||||
List<DateTime> dateTimes = [];
|
||||
foreach (string check in statementPeriodSegments)
|
||||
{
|
||||
if (!DateTime.TryParseExact(check, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
|
||||
continue;
|
||||
dateTimes.Add(dateTime);
|
||||
}
|
||||
if (dateTimes.Count != 2)
|
||||
logger.LogInformation($"Only {dateTimes.Count} date(s) were found in <{fileName}>!");
|
||||
else
|
||||
Move(file, fileName, checkFile, foundLines, dateTimes.AsReadOnly());
|
||||
}
|
||||
|
||||
private static void Move(ILogger<Worker> logger, string file, string checkFile, string dateFormat, string statementPeriod, string search)
|
||||
{
|
||||
List<string> foundLines = [];
|
||||
bool statementPeriodFound = false;
|
||||
string[]? statementPeriodSegments = null;
|
||||
string fileName = Path.GetFileName(file);
|
||||
string[] lines = File.ReadAllLines(file);
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (statementPeriodSegments is not null)
|
||||
{
|
||||
if (line.Contains(search))
|
||||
foundLines.Add(line);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (statementPeriodFound)
|
||||
{
|
||||
statementPeriodSegments = line.Split(' ');
|
||||
continue;
|
||||
}
|
||||
if (!line.Contains(statementPeriod))
|
||||
continue;
|
||||
statementPeriodFound = true;
|
||||
}
|
||||
}
|
||||
if (statementPeriodSegments is null || statementPeriodSegments.Length < 4)
|
||||
logger.LogInformation($"{nameof(statementPeriod)}: {statementPeriod}; wasn't found in <{fileName}>!");
|
||||
else
|
||||
Move(logger, dateFormat, file, checkFile, fileName, statementPeriodSegments.AsReadOnly(), foundLines);
|
||||
}
|
||||
|
||||
internal static void Move(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
string checkFile;
|
||||
string search = args[5];
|
||||
string dateFormat = args[3];
|
||||
string searchPatterns = args[2];
|
||||
string statementPeriod = args[4];
|
||||
string sourceDirectory = Path.GetFullPath(args[0]);
|
||||
string[] files = Directory.GetFiles(sourceDirectory, searchPatterns, SearchOption.AllDirectories);
|
||||
foreach (string file in files)
|
||||
{
|
||||
checkFile = Path.ChangeExtension(file, ".pdf");
|
||||
if (!File.Exists(checkFile))
|
||||
continue;
|
||||
Move(logger, file, checkFile, dateFormat, statementPeriod, search);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user