using Serilog; using System.Text; namespace File_Folder_Helper.Helpers; internal static class HelperSaveOrCopyContents { 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.Information("D) Directory, F) File or B) Both?"); ConsoleKey dfb = Console.ReadKey().Key; log.Information(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); } } } 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.Information(string.Concat(timeSpan.TotalSeconds, " TotalSeconds")); if (alongSideTopDirectoryOnly) { File.WriteAllText(filePathAndName, data.ToString()); log.Information("Data written"); } else if (clipboardTopDirectoryOnly) { TextCopy.ClipboardService.SetText(data.ToString()); log.Information("Data stored in clipboard"); } else if (alongSideAllDirectories) { File.AppendAllText(filePathAndName, "Done"); log.Information("Data written"); } else throw new Exception(); log.Information("Press any key to close"); } } }