Find files with matching patterns in a directory c#?

.net, c#, io, regex

Solution

You can put an OR in the regex :

string pattern = @"(23456780|otherpatt)";

Problem

``` string fileName = ""; string sourcePath = @"C:\vish"; string targetPath = @"C:\SR"; string sourceFile = System.IO.Path.Combine(sourcePath, fileName); string destFile = System.IO.Path.Combine(targetPath, fileName); string pattern = @"23456780"; var matches = Directory.GetFiles(@"c:\vish") .Where(path => Regex.Match(path, pattern).Success); foreach (string file in matches) { Console.WriteLine(file); fileName = System.IO.Path.GetFileName(file); Console.WriteLine(fileName); destFile = System.IO.Path.Combine(targetPath, fileName); System.IO.File.Copy(file, destFile, true); } ``` My above program works well with a single pattern. I'm using above program to find the files in a directory with a matching pattern but in my case I've multiple patterns so i need to pass multiple pattern in `string pattern` variable as an array but I don't have any idea how i can manipulate those pattern in Regex.Match. Can anyone help me?

Original source