Rename by replace
This commit is contained in:
parent
c37771da61
commit
02c03f2853
35
ADO2024/PI4/Helper-2024-12-12.cs
Normal file
35
ADO2024/PI4/Helper-2024-12-12.cs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace File_Folder_Helper.ADO2024.PI4;
|
||||||
|
|
||||||
|
internal static partial class Helper20241212
|
||||||
|
{
|
||||||
|
|
||||||
|
internal static void Rename(ILogger<Worker> logger, List<string> args)
|
||||||
|
{
|
||||||
|
string newFile;
|
||||||
|
string fileName;
|
||||||
|
string newFileName;
|
||||||
|
string directoryName;
|
||||||
|
string searchPattern = args[2];
|
||||||
|
string[] searchPatterns = args[3].Split('|');
|
||||||
|
string sourceDirectory = Path.GetFullPath(args[0]);
|
||||||
|
string[] files = Directory.GetFiles(sourceDirectory, searchPattern, SearchOption.AllDirectories);
|
||||||
|
if (files.Length == 0)
|
||||||
|
logger.LogError("No files found in {sourceDirectory} with search pattern {searchPattern}", sourceDirectory, searchPattern);
|
||||||
|
foreach (string file in files)
|
||||||
|
{
|
||||||
|
fileName = Path.GetFileName(file);
|
||||||
|
directoryName = Path.GetDirectoryName(file) ?? throw new Exception();
|
||||||
|
newFileName = fileName;
|
||||||
|
foreach (string pattern in searchPatterns)
|
||||||
|
newFileName = newFileName.Replace(pattern, "");
|
||||||
|
newFile = Path.Combine(directoryName, newFileName);
|
||||||
|
if (File.Exists(newFile))
|
||||||
|
logger.LogError("File {newFile} already exists", newFile);
|
||||||
|
else
|
||||||
|
File.Move(file, newFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -121,6 +121,8 @@ internal static class HelperDay
|
|||||||
ADO2024.PI4.Helper20241115.GetComplete(logger, args);
|
ADO2024.PI4.Helper20241115.GetComplete(logger, args);
|
||||||
else if (args[1] == "Day-Helper-2024-12-04")
|
else if (args[1] == "Day-Helper-2024-12-04")
|
||||||
ADO2024.PI4.Helper20241204.ConvertToUTF8(logger, args);
|
ADO2024.PI4.Helper20241204.ConvertToUTF8(logger, args);
|
||||||
|
else if (args[1] == "Day-Helper-2024-12-12")
|
||||||
|
ADO2024.PI4.Helper20241212.Rename(logger, args);
|
||||||
else
|
else
|
||||||
throw new Exception(appSettings.Company);
|
throw new Exception(appSettings.Company);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user