Program.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // See https://aka.ms/new-console-template for more information
  2. using System.Globalization;
  3. using System.Reflection.Emit;
  4. using TagLib;
  5. Console.WriteLine("Hello, World!");
  6. Environment.CurrentDirectory = "TargetFolder";
  7. var dirs = Directory.GetDirectories(".").Select(p => p.Substring(2)).Where(p => p.StartsWith("[") == false).ToArray();
  8. foreach (var dir in dirs)
  9. {
  10. Console.WriteLine();
  11. var firstFile = Directory.GetFiles(dir, "*.flac").FirstOrDefault();
  12. if (firstFile == null)
  13. {
  14. if (Directory.GetFiles(dir).Length == 0)
  15. {
  16. Grey($"* delete empty dir `{dir}'");
  17. Directory.Delete(dir, false);
  18. continue;
  19. }
  20. Warn($"* Warn: flac file not found in top-level dir, its in sub dir? `{dir}'");
  21. continue;
  22. }
  23. var tag = TagLib.File.Create(firstFile);
  24. var xc = (TagLib.Ogg.XiphComment)tag.GetTag(TagTypes.Xiph);
  25. var v = xc.GetField("DATE").FirstOrDefault()?.Trim();
  26. if (false == DateTime.TryParseExact(v, "yyyy.MM.dd", null, DateTimeStyles.None, out var dt))
  27. {
  28. Warn($"* Warn: unable to get date from `{firstFile}' in `{dir}'");
  29. continue;
  30. }
  31. tag.Dispose();
  32. var parts = dir.Split(' ');
  33. if (parts.Length <= 2)
  34. {
  35. Warn($"* Warn: unable to process folder name for `{dir}'");
  36. continue;
  37. }
  38. var last1 = parts[^1];
  39. var last2 = parts[^2];
  40. string prefix;
  41. string remain;
  42. if (last2.StartsWith('[') && last2.EndsWith("]") && last1.StartsWith('(') && last1.EndsWith(')'))
  43. {
  44. var evt = last1;
  45. var label = last2;
  46. prefix = $"[{dt:yyMMdd}] [{evt[1..^1]}] {label}";
  47. remain = string.Join(" ", parts[..^2]);
  48. }
  49. else if (last1.StartsWith('[') && last1.EndsWith(']'))
  50. {
  51. var label = last1;
  52. prefix = $"[{dt:yyMMdd}] {label}";
  53. remain = string.Join(" ", parts[..^1]);
  54. }
  55. else
  56. {
  57. prefix = $"[{dt:yyMMdd}]";
  58. remain = string.Join(" ", parts);
  59. Warn("* Warn: unable to get label");
  60. }
  61. Console.WriteLine($"from > {string.Empty.PadLeft(prefix.Length, ' ')}{dir}");
  62. Console.Write($"to > {prefix} ");
  63. HiLight($"{remain}");
  64. Console.WriteLine();
  65. var final = prefix + " " + remain;
  66. try
  67. {
  68. Directory.Move(dir, final);
  69. }
  70. catch (Exception e)
  71. {
  72. Error("Err:" + e.Message);
  73. }
  74. }
  75. Console.WriteLine();
  76. Console.Write("Finished. Press ENTER to exit");
  77. Console.ReadLine();
  78. static void HiLight(string msg) => ForegroundColor(ConsoleColor.Green, delegate { Console.Write(msg); });
  79. static void Grey(string msg) => ForegroundColor(ConsoleColor.DarkGray, delegate { Console.WriteLine(msg); });
  80. static void Warn(string msg) => ForegroundColor(ConsoleColor.Yellow, delegate { Console.WriteLine(msg); });
  81. static void Error(string msg) => ForegroundColor(ConsoleColor.Red, delegate { Console.WriteLine(msg); });
  82. static void ForegroundColor(ConsoleColor fg, Action action)
  83. {
  84. var oldColor = Console.ForegroundColor;
  85. Console.ForegroundColor = fg;
  86. try
  87. {
  88. action();
  89. }
  90. finally
  91. {
  92. Console.ForegroundColor = oldColor;
  93. }
  94. }