Hello Guys,
i am trying to search a folder of txt. files for any txt.file containing the name "TOM"
below is the code am trying to use
static void Main(string[] args)
{
string startFolder = @"c:\Textfiles\";
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);
IEnumerable
string searchTerm = @"people";
var queryMatchingFiles =
from file in fileList
where file.Extension == ".htm"
let fileText = GetFileText(file.FullName)
where fileText.Contains(searchTerm)
select file.FullName;
Console.WriteLine("The term \"{0}\" was found in:", searchTerm);
foreach (string filename in queryMatchingFiles)
{
Console.WriteLine(filename);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
static string GetFileText(string name)
{
string fileContents = String.Empty;
// If the file has been deleted since we took
// the snapshot, ignore it and return the empty string.
if (System.IO.File.Exists(name))
{
fileContents = System.IO.File.ReadAllText(name);
}
return fileContents;
}
}

Abhishek YadavPosted Jul 31, 2023, 2:45 PM
if (File.Exists(filename))
{
}
Cr BhargaviPosted Jul 31, 2023, 1:39 PM
The code you have provided seems to be searching for files with the ".htm" extension and checking if they contain the search term "people." However, your original requirement was to search for files containing the name "TOM" in txt files. Let's modify the code to search for "TOM" in txt files instead.
To search for "TOM" in txt files, you need to change the file extension filter from ".htm" to ".txt"
Mohammad HussainPosted Jul 31, 2023, 11:12 AM