code_junkie

Filtering file names: getting *.abc without *.abcd, or *.abcde, and so on

Nov 13th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. *
  2.  
  3. "*.abc" returns files having an extension of .abc, .abcd, .abcde, .abcdef, and so on.
  4. *
  5.  
  6. "*.abcd" returns only files having an extension of .abcd.
  7. *
  8.  
  9. "*.abcde" returns only files having an extension of .abcde.
  10. *
  11.  
  12. "*.abcdef" returns only files having an extension of .abcdef.
  13.  
  14. private bool StriktMatch(string fileExtension, string searchPattern)
  15. {
  16. bool isStriktMatch = false;
  17.  
  18. string extension = searchPattern.Substring(searchPattern.LastIndexOf('.'));
  19.  
  20. if (String.IsNullOrEmpty(extension))
  21. {
  22. isStriktMatch = true;
  23. }
  24. else if (extension.IndexOfAny(new char[] { '*', '?' }) != -1)
  25. {
  26. isStriktMatch = true;
  27. }
  28. else if (String.Compare(fileExtension, extension, true) == 0)
  29. {
  30. isStriktMatch = true;
  31. }
  32. else
  33. {
  34. isStriktMatch = false;
  35. }
  36.  
  37. return isStriktMatch;
  38. }
  39.  
  40. class Program
  41. {
  42. static void Main(string[] args)
  43. {
  44. string[] fileNames = Directory.GetFiles("C:\document", "*.abc");
  45.  
  46. ArrayList al = new ArrayList();
  47.  
  48. for (int i = 0; i < fileNames.Length; i++)
  49. {
  50. FileInfo file = new FileInfo(fileNames[i]);
  51. if (StriktMatch(file.Extension, "*.abc"))
  52. {
  53. al.Add(fileNames[i]);
  54. }
  55. }
  56.  
  57. fileNames = (String[])al.ToArray(typeof(String));
  58.  
  59. foreach (string s in fileNames)
  60. {
  61. Console.WriteLine(s);
  62. }
  63.  
  64. Console.Read();
  65. }
  66.  
  67. List<string> fileNames = new List<string>();
  68. // populate all filenames here with a Directory.GetFiles or whatever
  69.  
  70. string srcDir = "from"; // set this
  71. string destDir = "to"; // set this too
  72.  
  73. // this filters the names in the list to just those that end with ".doc"
  74. foreach (var f in fileNames.All(f => f.ToLower().EndsWith(".doc")))
  75. {
  76. try
  77. {
  78. File.Copy(Path.Combine(srcDir, f), Path.Combine(destDir, f));
  79. }
  80. catch { ... }
  81. }
  82.  
  83. string strSomePath = "c:\SomeFolder";
  84. string strSomePattern = "*.abc";
  85. string[] filez = Directory.GetFiles(strSomePath, strSomePattern);
  86.  
  87. var filtrd = from f in filez
  88. where f.EndsWith( strSomePattern )
  89. select f;
  90.  
  91. foreach (string strSomeFileName in filtrd)
  92. {
  93. Console.WriteLine( strSomeFileName );
  94. }
  95.  
  96. string[] fileList = Directory.GetFiles(path, "*.abc");
  97.  
  98. foreach (string file in fileList)
  99. {
  100. FileInfo fInfo = new FileInfo(file);
  101.  
  102. if (fInfo.Extension.Length == 4) // "." is counted in the length
  103. {
  104. // exact extension match - process the file...
  105. }
  106. }
Add Comment
Please, Sign In to add comment