FileSearch.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Abstractions;
  5. using System.Linq;
  6. namespace Quadarax.Foundation.Core.IO
  7. {
  8. public class FileSearch
  9. {
  10. #region *** Private fields ***
  11. private IFileSystem _fs;
  12. #endregion
  13. #region *** Properties ***
  14. public string Path { get; }
  15. public bool IsDirctoryRecursive { get; }
  16. #endregion
  17. #region *** Constructors ***
  18. public FileSearch(IFileSystem fileSystemAbstraction, string initialPath, bool includeSubdirs = true)
  19. {
  20. _fs = fileSystemAbstraction ?? throw new ArgumentNullException(nameof(fileSystemAbstraction));
  21. if (string.IsNullOrEmpty(initialPath))
  22. throw new ArgumentNullException(nameof(initialPath));
  23. if (!_fs.Directory.Exists(initialPath))
  24. throw new FileNotFoundException($"Initial path '{initialPath}' doesn't exists.");
  25. Path = initialPath;
  26. IsDirctoryRecursive = includeSubdirs;
  27. }
  28. public FileSearch(string initialPath, bool includeSubdirs = true) : this(new System.IO.Abstractions.FileSystem(),initialPath, includeSubdirs)
  29. {
  30. }
  31. #endregion
  32. #region *** Public operations ***
  33. public string[] Search()
  34. {
  35. return Search(new[] {"*"});
  36. }
  37. public string[] Search(IEnumerable<string> includePatterns, FileSearchComparer? comparer = null)
  38. {
  39. return Search(includePatterns, new List<string>(), comparer);
  40. }
  41. public string[] Search(IEnumerable<string> includePatterns, IEnumerable<string> excludePatterns, FileSearchComparer? comparer = null)
  42. {
  43. if (includePatterns == null)
  44. throw new ArgumentNullException(nameof(includePatterns));
  45. if (excludePatterns == null)
  46. throw new ArgumentNullException(nameof(excludePatterns));
  47. if (!includePatterns.Any())
  48. includePatterns = new List<string>(new[] {"*"});
  49. comparer ??= new FileSearchComparer(includePatterns, excludePatterns);
  50. return SearchFiles(Path,comparer);
  51. }
  52. #endregion
  53. #region *** Private Operations ***
  54. private string[] SearchFiles(string initialDirPath, FileSearchComparer comparer)
  55. {
  56. var result = new List<string>();
  57. // iterate files
  58. foreach (var file in _fs.Directory.GetFiles(initialDirPath))
  59. {
  60. // check if file match condition
  61. if (comparer.Match(file))
  62. result.Add(file);
  63. }
  64. if (IsDirctoryRecursive)
  65. {
  66. // iterate sub-dirs
  67. foreach (var currentDir in _fs.Directory.GetDirectories(initialDirPath))
  68. {
  69. // check if directory match condition
  70. if (comparer.Match(currentDir))
  71. result.AddRange(SearchFiles(currentDir, comparer));
  72. }
  73. }
  74. return result.ToArray();
  75. }
  76. #endregion
  77. }
  78. }