87 lines
3.7 KiB
C#
87 lines
3.7 KiB
C#
using KssSmaPlaLib.Batch.Interface;
|
|
using KssSmaPlaLib.Commons;
|
|
using KssSmaPlaLib.Ini;
|
|
using KssSmaPlaLib.IO.File;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static System.Net.WebRequestMethods;
|
|
|
|
namespace KssSmaPlaLib.Batch.IO
|
|
{
|
|
public class FileStepHandlerUxer
|
|
{
|
|
public FuncFileDeleteByIniResultDto TryFuncFileDeleteByIni(
|
|
string iniFilePath,
|
|
string sectionName)
|
|
{
|
|
const string KEY_FOLDER_PATH = "FolderPath";
|
|
const string KEY_FILE_NAME_PATTERNS = "FileNamePatterns";
|
|
|
|
FuncFileDeleteByIniResultDto result = new FuncFileDeleteByIniResultDto();
|
|
|
|
var iniDic = IniReferenceResolveUxer.ReadSectionWithReference(iniFilePath, sectionName);
|
|
|
|
var folderPath = DictionaryUxer.GetValueOrDefault(iniDic, KEY_FOLDER_PATH, string.Empty);
|
|
if (string.IsNullOrEmpty(folderPath))
|
|
{
|
|
var msg = $"設定ファイルのフォルダパスが指定されていません。[キー:{KEY_FOLDER_PATH}]";
|
|
LoggerUxer.Error(msg);
|
|
result.Succeeded = false;
|
|
result.ExitCode = (int)RetCodeUxer.RetCode.FileDeleteFolderPathNotSetting;
|
|
result.ResultMessage = msg;
|
|
return result;
|
|
}
|
|
if (!Directory.Exists(folderPath))
|
|
{
|
|
var msg = $"設定ファイルのフォルダパスが存在しません。[キー:{KEY_FOLDER_PATH},値:{folderPath}]";
|
|
LoggerUxer.Error(msg);
|
|
result.Succeeded = false;
|
|
result.ExitCode = (int)RetCodeUxer.RetCode.FileDeleteFolderPathNotExist;
|
|
result.ResultMessage = msg;
|
|
return result;
|
|
}
|
|
|
|
var fileNamePatterns = DictionaryUxer.GetValueOrDefault(iniDic, KEY_FILE_NAME_PATTERNS, string.Empty);
|
|
if (string.IsNullOrEmpty(fileNamePatterns))
|
|
{
|
|
var msg = $"設定ファイルのファイル名パターンが指定されていません。[キー:{KEY_FILE_NAME_PATTERNS}]";
|
|
LoggerUxer.Error(msg);
|
|
result.Succeeded = false;
|
|
result.ExitCode = (int)RetCodeUxer.RetCode.FileDeleteFileNamePatternNotSetting;
|
|
result.ResultMessage = msg;
|
|
return result;
|
|
}
|
|
var fileNamePatternsArray = fileNamePatterns.Split(',');
|
|
|
|
// ローカルのCSVファイルの削除
|
|
LoggerUxer.Info($"ファイルの削除を実行");
|
|
for (var cnt = 0; cnt < fileNamePatternsArray.Length; cnt++)
|
|
{
|
|
var fileNamePattern = fileNamePatternsArray[cnt];
|
|
LoggerUxer.Info($"[ファイル名パターン:{fileNamePattern}]");
|
|
if (!FileUxer.TryDeletePatternFiles(folderPath, fileNamePattern, out var fileCount, out var messageForMe))
|
|
{
|
|
LoggerUxer.Error(messageForMe);
|
|
result.Succeeded = false;
|
|
result.ExitCode = (int)RetCodeUxer.RetCode.FileDeletePatternFilesError;
|
|
result.ResultMessage = messageForMe;
|
|
return result;
|
|
}
|
|
LoggerUxer.Info($"[削除件数:{fileCount}]");
|
|
}
|
|
LoggerUxer.Info($"ローカルのCSVファイルの削除を完了");
|
|
result.Succeeded = true;
|
|
return result;
|
|
}
|
|
}
|
|
public class FuncFileDeleteByIniResultDto : IFuncResultDto
|
|
{
|
|
public bool Succeeded { get; set; } = false;
|
|
public int ExitCode { get; set; } = 0;
|
|
public string ResultMessage { get; set; } = string.Empty;
|
|
}
|
|
} |