FFS Rename

This commit is contained in:
Mike Phares 2024-08-05 13:02:37 -07:00
parent 2923e86a94
commit e440664659
3 changed files with 87 additions and 2 deletions

13
.vscode/launch.json vendored
View File

@ -12,8 +12,17 @@
"program": "${workspaceFolder}/bin/Debug/net8.0/win-x64/File-Folder-Helper.dll", "program": "${workspaceFolder}/bin/Debug/net8.0/win-x64/File-Folder-Helper.dll",
"args": [ "args": [
"s", "s",
"K", "X",
".kanbn" "D:/5-Other-Small/Free-File-Sync",
"Day-Helper-2024-08-05",
"*.ffs_gui",
"lines.md",
"C:/Users/phares/AppData/Roaming/FreeFileSync/GlobalSettings.xml",
"5555",
"6666",
"7777",
"8888",
"9999"
], ],
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"console": "integratedTerminal", "console": "integratedTerminal",

View File

@ -0,0 +1,74 @@
using Microsoft.Extensions.Logging;
namespace File_Folder_Helper.Day;
internal static partial class Helper20240805
{
private static void RenameFiles(ILogger<Worker> logger, string sourceDirectory, string[] files, string[] lines, string globalSettingsFile)
{
string to;
string old;
string from;
string what;
string concat;
string checkFile;
string extension;
string[] matches;
string[] segments;
string environment;
List<string> distinct = [];
string globalSettingsLines = File.ReadAllText(globalSettingsFile);
foreach (string line in lines)
{
segments = line.Split('|');
if (segments.Length != 7)
{
logger.LogWarning("Wrong length!");
continue;
}
to = segments[5].Trim();
old = segments[1].Trim();
from = segments[4].Trim();
what = segments[3].Trim();
environment = segments[2].Trim();
concat = string.Concat(environment, '-', what, '-', from, '-', to);
if (distinct.Contains(concat))
{
logger.LogWarning("Not distinct <{concat}>!", concat);
continue;
}
distinct.Add(concat);
globalSettingsLines = globalSettingsLines.Replace(old, concat);
matches = (from l in files where l.Contains(old) select l).ToArray();
if (matches.Length != 1)
{
logger.LogWarning("matches == {matchesLength} <{concat}>!", matches.Length, concat);
continue;
}
extension = Path.GetExtension(matches[0]);
checkFile = Path.Combine(sourceDirectory, $"{concat}{extension}");
File.Move(matches[0], checkFile);
}
File.WriteAllText(globalSettingsFile, globalSettingsLines);
}
internal static void RenameFiles(ILogger<Worker> logger, List<string> args)
{
string sourceDirectory = Path.GetFullPath(args[0]);
string globalSettingsFile = Path.GetFullPath(args[4]);
string[] files = Directory.GetFiles(sourceDirectory, args[2], SearchOption.TopDirectoryOnly);
string checkFile = Path.Combine(sourceDirectory, args[3]);
if (files.Length == 0 || !File.Exists(checkFile))
logger.LogWarning("No found!");
else
{
string[] lines = File.ReadAllLines(checkFile);
if (lines.Length == 0)
logger.LogWarning("No lines!");
else
RenameFiles(logger, sourceDirectory, files, lines, globalSettingsFile);
}
}
}

View File

@ -85,6 +85,8 @@ internal static class HelperDay
Day.Helper20240724.CopyDirectories(logger, args); Day.Helper20240724.CopyDirectories(logger, args);
else if (args[1] == "Day-Helper-2024-07-28") else if (args[1] == "Day-Helper-2024-07-28")
Day.Helper20240728.DownloadSslCertificates(logger, args); Day.Helper20240728.DownloadSslCertificates(logger, args);
else if (args[1] == "Day-Helper-2024-08-05")
Day.Helper20240805.RenameFiles(logger, args);
else else
throw new Exception(appSettings.Company); throw new Exception(appSettings.Company);
} }