SortCodeMethods
DeleteEmptyDirectories Remove deprecated tasks Current Results Move to 1 or 9 ...
This commit is contained in:
156
ADO2025/PI5/Helper-2025-03-21.cs
Normal file
156
ADO2025/PI5/Helper-2025-03-21.cs
Normal file
@ -0,0 +1,156 @@
|
||||
using File_Folder_Helper.Helpers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace File_Folder_Helper.ADO2025.PI5;
|
||||
|
||||
internal static partial class Helper20250321
|
||||
{
|
||||
|
||||
private record Record(string Directory,
|
||||
string File,
|
||||
ThreeDeep ThreeDeep)
|
||||
{
|
||||
|
||||
public static ReadOnlyCollection<Record> GetCollection(string sourceDirectory, string searchPattern, string[] files)
|
||||
{
|
||||
List<Record> results = [];
|
||||
Record record;
|
||||
string directory;
|
||||
string fileNameWithoutExtension;
|
||||
bool json = searchPattern.Contains(".json");
|
||||
bool check = searchPattern.Split('.').Length == 3;
|
||||
ReadOnlyCollection<ThreeDeep> collection = ThreeDeep.GetCollection(files);
|
||||
foreach (ThreeDeep threeDeep in collection)
|
||||
{
|
||||
if (!json && check)
|
||||
fileNameWithoutExtension = threeDeep.DirectoryName;
|
||||
else if (!json && !check)
|
||||
fileNameWithoutExtension = threeDeep.FileNameWithoutExtension;
|
||||
else if (json)
|
||||
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(threeDeep.FileNameWithoutExtension);
|
||||
else
|
||||
throw new NotImplementedException();
|
||||
directory = $"{fileNameWithoutExtension[^1]}{fileNameWithoutExtension[^3..][..2]}";
|
||||
if (json || (!json && !check))
|
||||
{
|
||||
record = new(Directory: Path.Combine(sourceDirectory, "new-a", directory),
|
||||
File: $"{threeDeep.FileNameWithoutExtension}{threeDeep.Extension}",
|
||||
ThreeDeep: threeDeep);
|
||||
}
|
||||
else if (!json && check)
|
||||
{
|
||||
record = new(Directory: Path.Combine(sourceDirectory, "new-b", directory, threeDeep.DirectoryName),
|
||||
File: $"{threeDeep.FileNameWithoutExtension}{threeDeep.Extension}",
|
||||
ThreeDeep: threeDeep);
|
||||
}
|
||||
else
|
||||
throw new NotImplementedException();
|
||||
results.Add(record);
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private record ThreeDeep(string Extension,
|
||||
string FileNameWithoutExtension,
|
||||
long LastModified,
|
||||
long Length,
|
||||
string DirectoryName,
|
||||
string ParentDirectoryName,
|
||||
string Root)
|
||||
{
|
||||
|
||||
public static ReadOnlyCollection<ThreeDeep> GetCollection(string[] files)
|
||||
{
|
||||
List<ThreeDeep> results = [];
|
||||
ThreeDeep record;
|
||||
FileInfo fileInfo;
|
||||
string parentDirectory;
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileInfo = new(file);
|
||||
parentDirectory = Path.GetDirectoryName(fileInfo.DirectoryName) ?? throw new Exception();
|
||||
record = new(Extension: Path.GetExtension(file),
|
||||
FileNameWithoutExtension: Path.GetFileNameWithoutExtension(file),
|
||||
LastModified: fileInfo.LastWriteTime.Ticks,
|
||||
Length: fileInfo.Length,
|
||||
DirectoryName: Path.GetFileName(fileInfo.DirectoryName) ?? throw new Exception(),
|
||||
ParentDirectoryName: Path.GetFileName(parentDirectory),
|
||||
Root: Path.GetDirectoryName(parentDirectory) ?? throw new Exception());
|
||||
results.Add(record);
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
public static string GetFullPath(ThreeDeep threeDeep) =>
|
||||
Path.Combine(threeDeep.Root, threeDeep.ParentDirectoryName, threeDeep.DirectoryName, $"{threeDeep.FileNameWithoutExtension}{threeDeep.Extension}");
|
||||
|
||||
}
|
||||
|
||||
internal static void MoveToLast(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
string[] searchPatterns = args[2].Split('|');
|
||||
string sourceDirectory = Path.GetFullPath(args[0]);
|
||||
if (searchPatterns.Length == 1)
|
||||
logger.LogInformation("No code for just one!");
|
||||
else
|
||||
{
|
||||
HelperDeleteEmptyDirectories.DeleteEmptyDirectories(logger, sourceDirectory);
|
||||
ReadOnlyCollection<Record> collection = GetCollection(logger, searchPatterns, sourceDirectory);
|
||||
if (collection.Count != 0)
|
||||
UseCollection(collection);
|
||||
else
|
||||
logger.LogInformation("No files!");
|
||||
if (collection.Count != 0)
|
||||
HelperDeleteEmptyDirectories.DeleteEmptyDirectories(logger, sourceDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<Record> GetCollection(ILogger<Worker> logger, string[] searchPatterns, string sourceDirectory)
|
||||
{
|
||||
string[] files;
|
||||
List<Record> results = [];
|
||||
foreach (string searchPattern in searchPatterns)
|
||||
{
|
||||
files = Directory.GetFiles(sourceDirectory, searchPattern, SearchOption.AllDirectories);
|
||||
if (files.Length == 0)
|
||||
logger.LogWarning("<{files}>(s)", files.Length);
|
||||
else
|
||||
{
|
||||
ReadOnlyCollection<Record> collection = Record.GetCollection(sourceDirectory, searchPattern, files);
|
||||
results.AddRange(collection);
|
||||
}
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
private static void UseCollection(ReadOnlyCollection<Record> collection)
|
||||
{
|
||||
string fullPath;
|
||||
string checkFile;
|
||||
List<string> distinct = [];
|
||||
foreach (Record record in collection)
|
||||
{
|
||||
if (distinct.Contains(record.Directory))
|
||||
continue;
|
||||
distinct.Add(record.Directory);
|
||||
}
|
||||
foreach (string directory in distinct)
|
||||
{
|
||||
if (Directory.Exists(directory))
|
||||
continue;
|
||||
_ = Directory.CreateDirectory(directory);
|
||||
}
|
||||
foreach (Record record in collection)
|
||||
{
|
||||
checkFile = Path.Combine(record.Directory, record.File);
|
||||
if (File.Exists(checkFile))
|
||||
continue;
|
||||
fullPath = ThreeDeep.GetFullPath(record.ThreeDeep);
|
||||
File.Move(fullPath, checkFile);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user