SupportFunctions.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace crusherScanner
  7. {
  8. internal static class SupportFunctions
  9. {
  10. /// <summary>
  11. /// Return a hash set of files in given folder.
  12. /// <code>
  13. /// GetFilesFromPath("c:\oreDefFiles\","ML??????*.csv");
  14. /// </code>
  15. /// </summary>
  16. /// <param name="Path">Path to search for files at.</param>
  17. /// <param name="Mask">Mask to check files against.</param>
  18. /// <returns>HashSet/<string/> of files in folder.</returns>
  19. public static HashSet<string> GetFilesFromPath(string Path,string Mask)
  20. {
  21. HashSet<string> files = new HashSet<string>();
  22. try
  23. {
  24. if (Directory.Exists(Path))
  25. {
  26. // Pattern match is for 'ML003092.csv' or 'ML003092_20220822.csv' = "ML??????*.csv"
  27. IEnumerable<string> jobFiles = Directory.EnumerateFiles(Path, Mask, SearchOption.TopDirectoryOnly);
  28. foreach (string jobFile in jobFiles)
  29. {
  30. files.Add(jobFile);
  31. }
  32. }
  33. else
  34. {
  35. Logging.Append(LogLevel.Debug, $"Unable to get files from non existant 'Path' {Path}.");
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. Logging.Append(LogLevel.Error ,"An error occurred." + ex.Message);
  41. }
  42. // return an empty HashSet if no files found
  43. return files;
  44. }
  45. }
  46. }