for Push.

This commit is contained in:
nobobo
2026-06-01 13:23:34 +09:00
parent 9e093ea99f
commit a6550e9928
93 changed files with 10393 additions and 0 deletions
+80
View File
@@ -0,0 +1,80 @@
using KssSmaPlaLib.Commons;
using KssSmaPlaLib.IO.File;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KssSmaPlaLib.Cleanup
{
public static class FileCleanupUxer
{
public static bool TryFileCleanupByDic(
Dictionary<string, string> iniDic,
out string resultMessage)
{
const string KEY_FOLDER_PATH = "FolderPath";
const string KEY_MAX_DAYS = "MaxDays";
const string KEY_MAX_FILE_COUNT = "MaxFileCount";
var folderPath = DictionaryUxer.GetValueOrDefault(iniDic, KEY_FOLDER_PATH, string.Empty);
if (string.IsNullOrEmpty(folderPath))
{
resultMessage = $"クリーンナップ処理用フォルダパスが設定されていません。[キー:{KEY_FOLDER_PATH}]";
return false;
}
if (!Directory.Exists(folderPath))
{
resultMessage = $"クリーンナップ処理用フォルダパスが存在しません。[キー:{KEY_FOLDER_PATH},値:{folderPath}]";
return false;
}
var str = DictionaryUxer.GetValueOrDefault(iniDic, KEY_MAX_DAYS, "99999");
if (!int.TryParse(str, out var maxDays) || maxDays < 0)
{
resultMessage = $"クリーンナップ処理用最大保持日数が不正です。[キー:{KEY_MAX_DAYS},値:{str}]";
return false;
}
LoggerUxer.Info($"[クリーンナップ対象フォルダパス:{folderPath}]");
LoggerUxer.Info($"[クリーンナップ最大保持日数:{maxDays}]");
var now = DateTime.Now;
var thresholdDate = now.AddDays(-(maxDays - 1)).Date;
LoggerUxer.Info($"[クリーンナップ保持再過去日:{thresholdDate:yyyy/MM/dd}]");
str = DictionaryUxer.GetValueOrDefault(iniDic, KEY_MAX_FILE_COUNT, "99999");
if (!int.TryParse(str, out var maxFileCount) || maxFileCount < 0)
{
resultMessage = $"クリーンナップ処理用最大保持ファイル数が不正です。[キー:{KEY_MAX_FILE_COUNT},値:{str}]";
return false;
}
LoggerUxer.Info($"[クリーンナップ最大保持ファイル数:{maxFileCount}]");
var ret = TryFileCleanup(folderPath, thresholdDate, maxFileCount, out resultMessage);
LoggerUxer.Info($"クリーンナップ処理が終了しました。");
return ret;
}
public static bool TryFileCleanup(string folderPath, DateTime thresholdDate, int maxFileCount, out string resultMessage)
{
resultMessage = string.Empty;
// フォルダの最大日数より過去のファイルを削除
if (!FileUxer.TryApplyActionToFiles<DateTime>(folderPath, f => f.LastWriteTime < thresholdDate, f => f.Delete(), out var deleteFileCount, out var messageForMe, "日数超過削除"))
{
resultMessage = $"バックアップフォルダ過去ファイル削除(期間制限):{messageForMe}";
return false;
}
// バックアップフォルダの最大ファイル数超過時に古いファイルから削除
if (!FileUxer.TryApplyActionToFiles<DateTime>(folderPath, f => true, f => f.Delete(), out deleteFileCount, out messageForMe, "ファイル数超過削除", true, null, maxFileCount))
{
resultMessage = $"バックアップフォルダ過去ファイル削除(ファイル数制限):{messageForMe}";
return false;
}
return true;
}
}
}