MoveFiles to Week of Year

This commit is contained in:
2024-09-10 17:07:46 -07:00
parent fdb1e32c82
commit db24568cb4
3 changed files with 51 additions and 10 deletions

View File

@ -99,6 +99,8 @@ internal static class HelperDay
Day.Q32024.Helper20240828.MoveWaferCounterToArchive(logger, args);
else if (args[1] == "Day-Helper-2024-08-30")
Day.Q32024.Helper20240830.CompareWorkItems(logger, args);
else if (args[1] == "Day-Helper-2024-09-10")
Day.Q32024.Helper20240910.MoveFilesToWeekOfYear(logger, args);
else
throw new Exception(appSettings.Company);
}

View File

@ -0,0 +1,39 @@
using Microsoft.Extensions.Logging;
using System.Globalization;
namespace File_Folder_Helper.Day.Q32024;
internal static partial class Helper20240910
{
internal static void MoveFilesToWeekOfYear(ILogger<Worker> logger, List<string> args)
{
string checkFile;
string weekOfYear;
string checkDirectory;
string searchPattern = args[2];
string sourceDirectory = args[0];
Calendar calendar = new CultureInfo("en-US").Calendar;
string[] files = Directory.GetFiles(sourceDirectory, searchPattern, SearchOption.AllDirectories);
logger.LogInformation("With search pattern '{SearchPattern}' found {files}", searchPattern, files.Length);
FileInfo[] collection = files.Select(l => new FileInfo(l)).ToArray();
foreach (FileInfo fileInfo in collection)
{
weekOfYear = $"{fileInfo.LastWriteTime.Year}_Week_{calendar.GetWeekOfYear(fileInfo.LastWriteTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
checkDirectory = Path.Combine(sourceDirectory, weekOfYear, fileInfo.LastWriteTime.ToString("yyyy-MM-dd"));
if (!Directory.Exists(checkDirectory))
_ = Directory.CreateDirectory(checkDirectory);
checkFile = Path.Combine(checkDirectory, fileInfo.Name);
if (checkFile == fileInfo.FullName)
continue;
try
{
if (File.Exists(checkFile))
continue;
File.Move(fileInfo.FullName, checkFile);
}
catch (Exception ex)
{ logger.LogInformation(ex, "Inner loop error!"); }
}
}
}