UpdateDateVerifyAndGetTicksDirectories
SaveOrCopyContents
This commit is contained in:
parent
f99802b7f4
commit
aa7469989a
@ -1,225 +1,73 @@
|
||||
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)
|
||||
{
|
||||
bool save;
|
||||
ConsoleKey? dfb;
|
||||
List<string> collection;
|
||||
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");
|
||||
long now = DateTime.Now.Ticks;
|
||||
string? parentDirectory = Path.GetDirectoryName(argsZero);
|
||||
if (string.IsNullOrEmpty(parentDirectory))
|
||||
throw new NotSupportedException();
|
||||
if (consoleKey == ConsoleKey.A)
|
||||
(dfb, save, searchOption) = (null, true, SearchOption.TopDirectoryOnly);
|
||||
else if (consoleKey == ConsoleKey.B)
|
||||
(dfb, save, searchOption) = (null, true, SearchOption.AllDirectories);
|
||||
else if (consoleKey == ConsoleKey.C)
|
||||
(dfb, save, searchOption) = (null, false, SearchOption.TopDirectoryOnly);
|
||||
else if (consoleKey == ConsoleKey.D)
|
||||
(dfb, save, searchOption) = (null, false, SearchOption.AllDirectories);
|
||||
else
|
||||
throw new NotSupportedException();
|
||||
for (int i = 0; i < short.MaxValue; i++)
|
||||
{
|
||||
if (dfb is null or not ConsoleKey.D and not ConsoleKey.F and not ConsoleKey.B)
|
||||
{
|
||||
log.LogInformation("D) Directory, F) File or B) Both?");
|
||||
dfb = Console.ReadKey().Key;
|
||||
log.LogInformation("{empty}", string.Empty);
|
||||
continue;
|
||||
}
|
||||
string fileName = dfb switch
|
||||
{
|
||||
ConsoleKey.D => "Directories",
|
||||
ConsoleKey.F => "Files",
|
||||
ConsoleKey.B => "Both",
|
||||
_ => throw new Exception(),
|
||||
_ => throw new NotSupportedException(),
|
||||
};
|
||||
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)
|
||||
if (dfb == ConsoleKey.F)
|
||||
collection = Directory.GetFiles(argsZero, "*", searchOption).ToList();
|
||||
else if (dfb == ConsoleKey.D)
|
||||
collection = Directory.GetDirectories(argsZero, "*", searchOption).ToList();
|
||||
else if (dfb == ConsoleKey.B)
|
||||
{
|
||||
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();
|
||||
collection = Directory.GetDirectories(argsZero, "*", searchOption).ToList();
|
||||
collection.AddRange(Directory.GetFiles(argsZero, "*", searchOption));
|
||||
}
|
||||
else
|
||||
throw new NotSupportedException();
|
||||
TimeSpan timeSpan = new(DateTime.Now.Ticks - now);
|
||||
log.LogInformation("{TotalSeconds} TotalSeconds", timeSpan.TotalSeconds);
|
||||
if (alongSideTopDirectoryOnly)
|
||||
if (save)
|
||||
{
|
||||
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");
|
||||
File.WriteAllLines(filePathAndName, collection);
|
||||
log.LogInformation("Data written");
|
||||
}
|
||||
else
|
||||
throw new Exception();
|
||||
{
|
||||
string text = string.Join(Environment.NewLine, collection);
|
||||
TextCopy.ClipboardService.SetText(text);
|
||||
log.LogInformation("Data stored in clipboard");
|
||||
}
|
||||
log.LogInformation("Press any key to close");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,88 @@ namespace File_Folder_Helper.Helpers;
|
||||
internal static class HelperTooLong
|
||||
{
|
||||
|
||||
private static void MoveFiles(string yearName, string personKeyFormattedDirectory, string personNameDirectory, string personNameDirectoryName)
|
||||
{
|
||||
string? checkFile;
|
||||
string? checkDirectory;
|
||||
string[] files = Directory.GetFiles(personNameDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
checkDirectory = Path.Combine(personKeyFormattedDirectory, yearName, personNameDirectoryName);
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
checkFile = Path.Combine(checkDirectory, Path.GetFileName(file));
|
||||
if (File.Exists(checkFile))
|
||||
continue;
|
||||
File.Move(file, checkFile);
|
||||
}
|
||||
}
|
||||
|
||||
private static void MaybeMoveFiles(Models.AppSettings appSettings, string[] ticksDirectories)
|
||||
{
|
||||
string yearName;
|
||||
string yearDirectoryName;
|
||||
string[] yearDirectories;
|
||||
string personKeyFormatted;
|
||||
string ticksDirectoryName;
|
||||
string personNameDirectoryName;
|
||||
string[] personNameDirectories;
|
||||
string[] personKeyFormattedDirectories;
|
||||
foreach (string ticksDirectory in ticksDirectories)
|
||||
{
|
||||
ticksDirectoryName = Path.GetFileName(ticksDirectory);
|
||||
if (!long.TryParse(ticksDirectoryName, out long directoryTicks))
|
||||
break;
|
||||
yearName = new DateTime(directoryTicks).Year.ToString();
|
||||
personKeyFormattedDirectories = Directory.GetDirectories(ticksDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string personKeyFormattedDirectory in personKeyFormattedDirectories)
|
||||
{
|
||||
personKeyFormatted = Path.GetFileName(personKeyFormattedDirectory);
|
||||
if (personKeyFormatted.Length != appSettings.PersonBirthdayFormat.Length)
|
||||
break;
|
||||
yearDirectories = Directory.GetDirectories(personKeyFormattedDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string yearDirectory in yearDirectories)
|
||||
{
|
||||
personNameDirectories = Directory.GetDirectories(yearDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
if (personNameDirectories.Length > 1)
|
||||
break;
|
||||
if (personNameDirectories.Length == 0)
|
||||
continue;
|
||||
personNameDirectoryName = Path.GetFileName(personNameDirectories[0]);
|
||||
if (!personNameDirectoryName.StartsWith("X+") && personNameDirectoryName.Length != 1)
|
||||
break;
|
||||
yearDirectoryName = Path.GetFileName(yearDirectory);
|
||||
if (yearDirectoryName == yearName)
|
||||
continue;
|
||||
MoveFiles(yearName, personKeyFormattedDirectory, personNameDirectories[0], personNameDirectoryName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static void UpdateDateVerifyAndGetTicksDirectories(Models.AppSettings appSettings, string directory)
|
||||
{
|
||||
string ticksDirectoryName;
|
||||
DirectoryInfo directoryInfo;
|
||||
string[] ticksDirectories = Directory.GetDirectories(directory, "*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string ticksDirectory in ticksDirectories)
|
||||
{
|
||||
ticksDirectoryName = Path.GetFileName(ticksDirectory);
|
||||
if (ticksDirectoryName.Length < 3)
|
||||
break;
|
||||
if (!long.TryParse(ticksDirectoryName, out long directoryTicks))
|
||||
break;
|
||||
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));
|
||||
}
|
||||
MaybeMoveFiles(appSettings, ticksDirectories);
|
||||
for (int i = 1; i < 10; i++)
|
||||
_ = HelperDeleteEmptyDirectories.DeleteEmptyDirectories(directory);
|
||||
}
|
||||
|
||||
internal static void TooLong(string workingDirectory, bool delete)
|
||||
{
|
||||
string[] files;
|
||||
|
14
Worker.cs
14
Worker.cs
@ -95,8 +95,8 @@ public class Worker : BackgroundService
|
||||
_Logger.LogInformation("B) Save (All Directories),");
|
||||
_Logger.LogInformation("C) Clipboard (Top Directory Only),");
|
||||
_Logger.LogInformation("D) Clipboard (All Directories),");
|
||||
_Logger.LogInformation("E) Clipboard (Top Directory Only and File Name Without Extension),");
|
||||
_Logger.LogInformation("F) Clipboard (All Directories and File Name Without Extension),");
|
||||
// E
|
||||
// F
|
||||
_Logger.LogInformation("G) Genealogical Data Communication");
|
||||
_Logger.LogInformation("H) Hardcoded file search and sort,");
|
||||
_Logger.LogInformation("I) Ignore case and rename files to lowercase,");
|
||||
@ -110,7 +110,7 @@ public class Worker : BackgroundService
|
||||
// Q
|
||||
_Logger.LogInformation("R) Rename to old, copy, delete old,");
|
||||
_Logger.LogInformation("S) Set Date from Zip Entry,");
|
||||
_Logger.LogInformation("T) Too long rename,");
|
||||
_Logger.LogInformation("T) *Ticks ~~Too long rename~~,");
|
||||
_Logger.LogInformation("U) Links for Hugo,");
|
||||
_Logger.LogInformation("V) VSCode Hope Sort,");
|
||||
// W
|
||||
@ -126,8 +126,7 @@ public class Worker : BackgroundService
|
||||
case ConsoleKey.A:
|
||||
case ConsoleKey.B:
|
||||
case ConsoleKey.C:
|
||||
case ConsoleKey.E:
|
||||
case ConsoleKey.F:
|
||||
case ConsoleKey.D:
|
||||
Helpers.HelperSaveOrCopyContents.SaveOrCopyContents(_Logger, _Args[0], consoleKey);
|
||||
break;
|
||||
case ConsoleKey.G:
|
||||
@ -170,8 +169,9 @@ public class Worker : BackgroundService
|
||||
_ = Helpers.HelperZipFilesByDate.SetDateFromZipEntry(_Logger, _Args[0]);
|
||||
break;
|
||||
case ConsoleKey.T:
|
||||
Helpers.HelperTooLong.TooLong(_Args[0], delete: false);
|
||||
Helpers.HelperTooLong.TooLong(_Args[0], delete: true);
|
||||
Helpers.HelperTooLong.UpdateDateVerifyAndGetTicksDirectories(_AppSettings, _Args[0]);
|
||||
// Helpers.HelperTooLong.TooLong(_Args[0], delete: false);
|
||||
// Helpers.HelperTooLong.TooLong(_Args[0], delete: true);
|
||||
break;
|
||||
case ConsoleKey.U:
|
||||
Helpers.HelperMarkdown.MarkdownConvertLinksForHugo(_AppSettings, _Logger, _Args);
|
||||
|
Loading…
x
Reference in New Issue
Block a user