80 lines
3.2 KiB
C#
80 lines
3.2 KiB
C#
using File_Folder_Helper.Helpers;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace File_Folder_Helper.ADO2024.PI2;
|
|
|
|
internal static partial class Helper20240519
|
|
{
|
|
|
|
private record Record(long Length, long Ticks);
|
|
|
|
private static ReadOnlyDictionary<string, Record> GetKeyValuePairs(string source, string[] sourceFiles)
|
|
{
|
|
Dictionary<string, Record> results = [];
|
|
string key;
|
|
Record? record;
|
|
FileInfo fileInfo;
|
|
int sourceLength = source.Length;
|
|
foreach (string sourceFile in sourceFiles)
|
|
{
|
|
fileInfo = new(sourceFile);
|
|
key = sourceFile[sourceLength..];
|
|
if (results.TryGetValue(key, out record))
|
|
throw new NotSupportedException();
|
|
results.Add(key, new(fileInfo.Length, fileInfo.LastWriteTime.Ticks));
|
|
}
|
|
return new(results);
|
|
}
|
|
|
|
internal static void FindReplaceDirectoryName(ILogger<Worker> logger, List<string> args)
|
|
{
|
|
string checkDirectory;
|
|
string replaceText = args[3];
|
|
string[] findTexts = args[2].Split(',');
|
|
string root = Path.GetFullPath(args[0]);
|
|
string[] directories = Directory.GetDirectories(root, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (string directory in directories)
|
|
{
|
|
checkDirectory = directory;
|
|
foreach (string findText in findTexts)
|
|
checkDirectory = checkDirectory.Replace(findText, replaceText);
|
|
if (checkDirectory == directory)
|
|
continue;
|
|
if (Directory.Exists(checkDirectory))
|
|
continue;
|
|
logger.LogInformation("<{directory}> to <{checkDirectory}>", directory, checkDirectory);
|
|
Directory.Move(directory, checkDirectory);
|
|
}
|
|
string key;
|
|
Record? record;
|
|
string checkFile;
|
|
FileInfo fileInfo;
|
|
string target = Path.GetFullPath(args[6]);
|
|
string source = Path.GetFullPath(args[4]);
|
|
string compare = Path.GetFullPath(args[5]);
|
|
string[] sourceFiles = Directory.GetFiles(source, "*", SearchOption.AllDirectories);
|
|
ReadOnlyDictionary<string, Record> keyValuePairs = GetKeyValuePairs(source, sourceFiles);
|
|
string[] compareFiles = Directory.GetFiles(compare, "*", SearchOption.AllDirectories);
|
|
int compareLength = compare.Length;
|
|
foreach (string compareFile in compareFiles)
|
|
{
|
|
fileInfo = new(compareFile);
|
|
key = compareFile[compareLength..];
|
|
if (!keyValuePairs.TryGetValue(key, out record))
|
|
continue;
|
|
if (fileInfo.Length != record.Length || fileInfo.LastWriteTime.Ticks != record.Ticks)
|
|
continue;
|
|
checkFile = $"{target}{key}";
|
|
checkDirectory = Path.GetDirectoryName(checkFile) ?? throw new NotSupportedException();
|
|
if (!Directory.Exists(checkDirectory))
|
|
_ = Directory.CreateDirectory(checkDirectory);
|
|
if (File.Exists(checkFile))
|
|
continue;
|
|
logger.LogInformation("<{compareFile}> to <{checkFile}>", compareFile, checkFile);
|
|
File.Move(compareFile, checkFile);
|
|
}
|
|
HelperDeleteEmptyDirectories.DeleteEmptyDirectories(logger, compare);
|
|
}
|
|
|
|
} |