I have successfully regex matched multiple string from a folder with txt.files with "streamreader" but i also need to obtain the matched string's file path. How am i able to obtain the matched string's file paths?
static void abnormalitiescheck()
{
int count = 0;
Regex regex = new Regex(@"(@@@@@)");
DirectoryInfo di = new DirectoryInfo(txtpath);
Console.WriteLine("No" + "\t" + "Name and location of file" + "\t" + "||" +" " + "Abnormal Text Detected");
Console.WriteLine("=" + "\t" + "=========================" + "\t" + "||" + " " + "=======================");
foreach (string files in Directory.GetFiles(txtpath, "*.txt"))
{
using (StreamReader reader = new StreamReader(files))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Match match = regex.Match(line);
if (match.Success)
{
count++;
Console.WriteLine(count + "\t\t\t\t\t" + match.Value + "\n");
}
}
}
}
}
If possible , i want to have output of the strings's file path as well. For e.g.,
C:/..../email_4.txt
C:/..../email_7.txt
C:/..../email_8.txt
C:/..../email_9.txt
As you already have the DirectoryInfo
, you could get the FullName property.
You also have the filename called files
. To get the name and location of the file, you could use Path.Combine
Your updated code could look like:
Console.WriteLine(count + "\t" + Path.Combine(di.FullName , Path.GetFileName(files)) + "\t" + match.Value + "\n");