Program.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.IO;
  3. namespace DirEx
  4. {
  5. internal class Program
  6. {
  7. private static int Main(string[] args)
  8. {
  9. if (args.Length == 0)
  10. {
  11. Console.Error.WriteLine("Usage DirEx </f|/d> [/day:<num>] [/dir:<path>] [/pattern:<pattern>] /s");
  12. Console.Error.WriteLine(" /f limit result to files");
  13. Console.Error.WriteLine(" /d limit result to directories");
  14. Console.Error.WriteLine(" /day:<num> limit to older <num> days");
  15. Console.Error.WriteLine(" /dir:<path> path to list, default on working directory");
  16. Console.Error.WriteLine(" /pattern:<pattern> filter pattern default is *");
  17. Console.Error.WriteLine(" /s search in sub directories");
  18. Environment.ExitCode = -1;
  19. Environment.Exit(1);
  20. return -1;
  21. }
  22. var dir = Environment.CurrentDirectory;
  23. var showFile = false;
  24. var showDir = false;
  25. DateTime? day = null;
  26. var pattern = "*";
  27. SearchOption option = SearchOption.TopDirectoryOnly;
  28. foreach (var arg in args)
  29. {
  30. var argKv = arg.Split(new[] { ':' }, 2);
  31. var k = argKv[0].ToLower();
  32. var v = argKv.Length == 2 ? argKv[1] : null;
  33. switch (k)
  34. {
  35. default:
  36. Console.Error.WriteLine($"Unrecognizable param: {k}");
  37. Environment.ExitCode = -1;
  38. Environment.Exit(1);
  39. return -1;
  40. case "/dir":
  41. if (string.IsNullOrWhiteSpace(v))
  42. {
  43. Console.Error.WriteLine("Missing argument in /dir");
  44. Environment.ExitCode = -1;
  45. Environment.Exit(1);
  46. return -1;
  47. }
  48. dir = v;
  49. break;
  50. case "/f": showFile = true; break;
  51. case "/d": showDir = true; break;
  52. case "/pattern":
  53. if (string.IsNullOrWhiteSpace(v))
  54. {
  55. Console.Error.WriteLine("Missing argument in /pattern");
  56. Environment.ExitCode = -1;
  57. Environment.Exit(1);
  58. return -1;
  59. }
  60. pattern = v;
  61. break;
  62. case "/day":
  63. if (string.IsNullOrWhiteSpace(v))
  64. {
  65. Console.Error.WriteLine("Missing argument in /day");
  66. Environment.ExitCode = -1;
  67. Environment.Exit(1);
  68. return -1;
  69. }
  70. if (int.TryParse(v, out var days))
  71. {
  72. day = DateTime.Now.AddDays(-days);
  73. }
  74. else
  75. {
  76. Console.Error.WriteLine("Invalid argument in /day");
  77. Environment.ExitCode = -1;
  78. Environment.Exit(1);
  79. return -1;
  80. }
  81. break;
  82. case "/s":
  83. option = SearchOption.AllDirectories;
  84. break;
  85. }
  86. }
  87. if (showFile && showDir)
  88. {
  89. Console.Error.WriteLine("Conflicted between /f and /d");
  90. Environment.ExitCode = -1;
  91. Environment.Exit(1);
  92. return -1;
  93. }
  94. if (!showFile && !showDir)
  95. {
  96. Console.Error.WriteLine("Missing /f or /d");
  97. Environment.ExitCode = -1;
  98. Environment.Exit(1);
  99. return -1;
  100. }
  101. if (showFile)
  102. {
  103. var entries = Directory.GetFiles(dir, pattern, option);
  104. foreach (var item in entries)
  105. {
  106. if (day.HasValue)
  107. {
  108. var info = new FileInfo(item);
  109. if (info.LastWriteTime < day) Console.WriteLine(item);
  110. }
  111. else
  112. {
  113. Console.WriteLine(item);
  114. }
  115. }
  116. }
  117. if (showDir)
  118. {
  119. var entries = Directory.GetDirectories(dir, pattern, option);
  120. foreach (var item in entries)
  121. {
  122. if (day.HasValue)
  123. {
  124. var info = new DirectoryInfo(item);
  125. if (info.LastWriteTime < day) Console.WriteLine(item);
  126. }
  127. else
  128. {
  129. Console.WriteLine(item);
  130. }
  131. }
  132. }
  133. return 0;
  134. }
  135. }
  136. }