StartAt and Destination are supplied nuget-lower HelperGenealogicalDataCommunication
242 lines
11 KiB
C#
242 lines
11 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Text;
|
|
|
|
namespace File_Folder_Helper.Helpers;
|
|
|
|
internal static class HelperSaveOrCopyContents
|
|
{
|
|
|
|
private record TicksDirectory(string Directory,
|
|
string DirectoryName,
|
|
long DirectoryTicks,
|
|
float? TotalDays);
|
|
|
|
private static List<TicksDirectory> UpdateDateVerifyAndGetTicksDirectories(string directory)
|
|
{
|
|
List<TicksDirectory> results = new();
|
|
float? totalDays;
|
|
string ticksDirectoryName;
|
|
DirectoryInfo directoryInfo;
|
|
long? lastDirectoryTicks = null;
|
|
DateTime dateTime = DateTime.Now;
|
|
string[] ticksDirectories = Directory.GetDirectories(directory, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (string ticksDirectory in ticksDirectories)
|
|
{
|
|
ticksDirectoryName = Path.GetFileName(ticksDirectory);
|
|
if (ticksDirectoryName.Length < 3 || ticksDirectoryName.First() != '(' || ticksDirectoryName[^1] != ')')
|
|
continue;
|
|
if (!long.TryParse(ticksDirectoryName[1..^1], out long directoryTicks))
|
|
{
|
|
if (!long.TryParse(ticksDirectoryName[1..^4], out directoryTicks))
|
|
throw new NotSupportedException();
|
|
}
|
|
directoryInfo = new(ticksDirectory);
|
|
if (directoryInfo.CreationTime.Ticks != directoryTicks)
|
|
Directory.SetCreationTime(ticksDirectory, new DateTime(directoryTicks));
|
|
if (directoryInfo.LastWriteTime.Ticks != directoryTicks)
|
|
Directory.SetLastWriteTime(ticksDirectory, new DateTime(directoryTicks));
|
|
totalDays = lastDirectoryTicks is null || new TimeSpan(dateTime.Ticks - directoryTicks).TotalDays < 1 ? null : (float)new TimeSpan(directoryTicks - lastDirectoryTicks.Value).TotalDays;
|
|
results.Add(new(ticksDirectory, ticksDirectoryName, directoryTicks, totalDays));
|
|
lastDirectoryTicks = directoryTicks;
|
|
}
|
|
string[] compare = (from l in results where l.TotalDays is not null and < 3.95f select l.Directory).ToArray();
|
|
if (compare.Any())
|
|
throw new Exception($"Please Consolidate <{string.Join(Environment.NewLine, compare)}>");
|
|
return results;
|
|
}
|
|
|
|
internal static void SaveOrCopyContents(ILogger log, string argsZero, ConsoleKey consoleKey)
|
|
{
|
|
SearchOption searchOption;
|
|
bool fileNameWithoutExtension;
|
|
bool alongSideTopDirectoryOnly = false;
|
|
bool alongSideAllDirectories = false;
|
|
bool clipboardTopDirectoryOnly = false;
|
|
switch (consoleKey)
|
|
{
|
|
case ConsoleKey.A:
|
|
alongSideTopDirectoryOnly = true;
|
|
searchOption = SearchOption.TopDirectoryOnly;
|
|
fileNameWithoutExtension = false;
|
|
break;
|
|
case ConsoleKey.B:
|
|
alongSideAllDirectories = true;
|
|
searchOption = SearchOption.AllDirectories;
|
|
fileNameWithoutExtension = false;
|
|
break;
|
|
case ConsoleKey.C:
|
|
clipboardTopDirectoryOnly = true;
|
|
searchOption = SearchOption.TopDirectoryOnly;
|
|
fileNameWithoutExtension = false;
|
|
break;
|
|
case ConsoleKey.D:
|
|
clipboardTopDirectoryOnly = true;
|
|
searchOption = SearchOption.AllDirectories;
|
|
fileNameWithoutExtension = false;
|
|
break;
|
|
case ConsoleKey.E:
|
|
clipboardTopDirectoryOnly = true;
|
|
searchOption = SearchOption.TopDirectoryOnly;
|
|
fileNameWithoutExtension = true;
|
|
break;
|
|
case ConsoleKey.F:
|
|
clipboardTopDirectoryOnly = true;
|
|
searchOption = SearchOption.AllDirectories;
|
|
fileNameWithoutExtension = true;
|
|
break;
|
|
default:
|
|
throw new Exception();
|
|
}
|
|
log.LogInformation("D) Directory, F) File or B) Both?");
|
|
ConsoleKey dfb = Console.ReadKey().Key;
|
|
log.LogInformation("{empty}", string.Empty);
|
|
if (dfb is not ConsoleKey.D and not ConsoleKey.F and not ConsoleKey.B)
|
|
throw new Exception("Not valid");
|
|
else
|
|
{
|
|
string fileName = dfb switch
|
|
{
|
|
ConsoleKey.D => "Directories",
|
|
ConsoleKey.F => "Files",
|
|
ConsoleKey.B => "Both",
|
|
_ => throw new Exception(),
|
|
};
|
|
long now = DateTime.Now.Ticks;
|
|
StringBuilder data = new();
|
|
string[] dataCollection;
|
|
string? parentDirectory = Path.GetDirectoryName(argsZero);
|
|
if (string.IsNullOrEmpty(parentDirectory))
|
|
throw new Exception();
|
|
string filePathAndName = Path.Combine(parentDirectory, $"{fileName}.txt");
|
|
if (alongSideTopDirectoryOnly)
|
|
File.WriteAllText(filePathAndName, string.Empty);
|
|
else if (alongSideAllDirectories)
|
|
File.WriteAllText(filePathAndName, string.Concat(argsZero, Environment.NewLine, "Start", Environment.NewLine));
|
|
switch (dfb)
|
|
{
|
|
case ConsoleKey.D:
|
|
if (alongSideTopDirectoryOnly || clipboardTopDirectoryOnly)
|
|
{
|
|
dataCollection = Directory.GetDirectories(argsZero, "*", searchOption);
|
|
if (!(dataCollection == null) && dataCollection.Length > 0)
|
|
{
|
|
foreach (string s in dataCollection)
|
|
{
|
|
if (fileNameWithoutExtension)
|
|
_ = data.Append('\'').Append(Path.GetFileNameWithoutExtension(s)).Append("', ");
|
|
else
|
|
_ = data.AppendLine(s);
|
|
}
|
|
}
|
|
if (clipboardTopDirectoryOnly && consoleKey == ConsoleKey.C)
|
|
_ = UpdateDateVerifyAndGetTicksDirectories(argsZero);
|
|
}
|
|
else if (alongSideAllDirectories)
|
|
{
|
|
dataCollection = Directory.GetDirectories(argsZero, "*", searchOption);
|
|
if (!(dataCollection == null) && dataCollection.Length > 0)
|
|
{
|
|
foreach (string s in dataCollection)
|
|
File.AppendAllText(filePathAndName, string.Concat(s.Replace(argsZero, "../"), Environment.NewLine));
|
|
}
|
|
}
|
|
else
|
|
throw new Exception();
|
|
break;
|
|
case ConsoleKey.F:
|
|
if (alongSideTopDirectoryOnly || clipboardTopDirectoryOnly)
|
|
{
|
|
dataCollection = Directory.GetFiles(argsZero, "*", searchOption);
|
|
if (!(dataCollection == null) && dataCollection.Length > 0)
|
|
{
|
|
foreach (string s in dataCollection)
|
|
{
|
|
if (fileNameWithoutExtension)
|
|
_ = data.Append('\'').Append(Path.GetFileNameWithoutExtension(s)).Append("', ");
|
|
else
|
|
_ = data.AppendLine(s);
|
|
}
|
|
}
|
|
}
|
|
else if (alongSideAllDirectories)
|
|
{
|
|
dataCollection = Directory.GetFiles(argsZero, "*", searchOption);
|
|
if (!(dataCollection == null) && dataCollection.Length > 0)
|
|
{
|
|
foreach (string s in dataCollection)
|
|
File.AppendAllText(filePathAndName, string.Concat(s.Replace(argsZero, "../"), Environment.NewLine));
|
|
}
|
|
}
|
|
else
|
|
throw new Exception();
|
|
break;
|
|
case ConsoleKey.B:
|
|
if (alongSideTopDirectoryOnly || clipboardTopDirectoryOnly)
|
|
{
|
|
dataCollection = Directory.GetFiles(argsZero, "*", searchOption);
|
|
if (!(dataCollection == null) && dataCollection.Length > 0)
|
|
{
|
|
foreach (string s in dataCollection)
|
|
{
|
|
if (fileNameWithoutExtension)
|
|
_ = data.Append('\'').Append(Path.GetFileNameWithoutExtension(s)).Append("', ");
|
|
else
|
|
_ = data.AppendLine(s);
|
|
}
|
|
}
|
|
}
|
|
else if (alongSideAllDirectories)
|
|
{
|
|
dataCollection = Directory.GetDirectories(argsZero, "*", searchOption);
|
|
if (!(dataCollection == null) && dataCollection.Length > 0)
|
|
{
|
|
foreach (string s in dataCollection)
|
|
File.AppendAllText(filePathAndName, string.Concat(s.Replace(argsZero, "../"), Environment.NewLine));
|
|
}
|
|
}
|
|
else
|
|
throw new Exception();
|
|
break;
|
|
default:
|
|
throw new Exception();
|
|
}
|
|
TimeSpan timeSpan = new(DateTime.Now.Ticks - now);
|
|
log.LogInformation("{TotalSeconds} TotalSeconds", timeSpan.TotalSeconds);
|
|
if (alongSideTopDirectoryOnly)
|
|
{
|
|
File.WriteAllText(filePathAndName, data.ToString());
|
|
log.LogInformation("Data written");
|
|
}
|
|
else if (clipboardTopDirectoryOnly)
|
|
{
|
|
TextCopy.ClipboardService.SetText(data.ToString());
|
|
log.LogInformation("Data stored in clipboard");
|
|
}
|
|
else if (alongSideAllDirectories)
|
|
{
|
|
File.AppendAllText(filePathAndName, "Done");
|
|
log.LogInformation("Data written");
|
|
}
|
|
else
|
|
throw new Exception();
|
|
log.LogInformation("Press any key to close");
|
|
}
|
|
}
|
|
|
|
internal static void IgnoreCaseAndRenameFilesToLowercase(ILogger log, string argsZero)
|
|
{
|
|
string fileName;
|
|
int filesRenamed = 0;
|
|
string[] files = Directory.GetFiles(argsZero, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (string file in files)
|
|
{
|
|
fileName = Path.GetFileName(file);
|
|
if (fileName == fileName.ToLower())
|
|
continue;
|
|
File.Move(file, file.ToLower());
|
|
filesRenamed++;
|
|
}
|
|
log.LogInformation("{filesRenamed}(s) renamed", filesRenamed);
|
|
}
|
|
|
|
} |