84 lines
2.8 KiB
C#
84 lines
2.8 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Globalization;
|
|
|
|
namespace File_Folder_Helper.Helpers;
|
|
|
|
internal static class HelperHardcodedFileSearchAndSort
|
|
{
|
|
|
|
internal static void HardcodedFileSearchAndSort(ILogger log, string sourceDirectory, SearchOption searchOption = SearchOption.TopDirectoryOnly)
|
|
{
|
|
bool check;
|
|
string lines;
|
|
string checkFile;
|
|
string? directory;
|
|
FileInfo fileInfo;
|
|
string weekOfYear;
|
|
string checkDirectory;
|
|
CultureInfo cultureInfo = new("en-US");
|
|
Calendar calendar = cultureInfo.Calendar;
|
|
string[] hardcodedValues = new string[]
|
|
{
|
|
"BIORAD2",
|
|
"BIORAD3",
|
|
"BIORAD4",
|
|
"BIORAD5",
|
|
"CDE2",
|
|
"CDE3",
|
|
"CDE4",
|
|
"CDE5",
|
|
"CDE6",
|
|
"HGCV1",
|
|
"HGCV2",
|
|
"HGCV3",
|
|
"TENCOR1",
|
|
"TENCOR2",
|
|
"TENCOR3",
|
|
"SP101",
|
|
"SPV01",
|
|
"SRP",
|
|
"WC6Inch",
|
|
"WC8Inch",
|
|
"Bio-Rad"
|
|
};
|
|
string[] files = Directory.GetFiles(sourceDirectory, "*", searchOption);
|
|
foreach (string file in files)
|
|
{
|
|
directory = Path.GetDirectoryName(file);
|
|
if (string.IsNullOrEmpty(directory))
|
|
continue;
|
|
check = false;
|
|
fileInfo = new(file);
|
|
weekOfYear = calendar.GetWeekOfYear(fileInfo.LastWriteTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
|
for (int i = 1; i < 3; i++)
|
|
{
|
|
if (check)
|
|
break;
|
|
lines = i switch
|
|
{
|
|
1 => fileInfo.Name,
|
|
2 => File.ReadAllText(file),
|
|
_ => throw new NotImplementedException()
|
|
};
|
|
foreach (string hardcodedValue in hardcodedValues)
|
|
{
|
|
if (!lines.Contains(hardcodedValue))
|
|
continue;
|
|
checkDirectory = Path.Combine(directory, $"{fileInfo.LastWriteTime:yyyy}_Week_{weekOfYear}", fileInfo.LastWriteTime.ToString("yyyy-MM-dd"), hardcodedValue);
|
|
if (!Directory.Exists(checkDirectory))
|
|
_ = Directory.CreateDirectory(checkDirectory);
|
|
checkFile = Path.Combine(checkDirectory, Path.GetFileName(file));
|
|
if (File.Exists(checkFile) || !File.Exists(file))
|
|
continue;
|
|
try
|
|
{ File.Move(file, checkFile); }
|
|
catch (Exception) { }
|
|
check = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
log.LogInformation("{sourceDirectory}", sourceDirectory);
|
|
}
|
|
|
|
} |