Search text files for string 
July 5th, 2009, 03:26 PM
| | Newbie | | Join Date: Aug 2008
Posts: 7
| |
Hi all
This sounds easy and I'm sure it is but i'm just having so much trouble. I have a directory with text files. I want to search the text files for a string and return the name of the file that containes the string. I've tried various way below ut unsuccessfully. Please could someone take a look and offer some assistance?
thanks -
-
string directory = @"D:\SearchFolder";
-
DirectoryInfo dirinfo = new DirectoryInfo(directory);
-
-
FileInfo[] files = dirinfo.GetFiles("EE*.PRN");
-
-
foreach (FileInfo i in files)
-
{
-
List<string> lines = File.ReadAllLines(i.ToString().ToList());
-
foreach (string line in lines)
-
{
-
if (line.Contains("UNIQUESTRING"))
-
textBox2.Text += i + "contains" + line.ToString() + Environment.NewLine;
-
}
-
-
//Regex.Match(i, "UNIQUESTRING");
-
//if (Regex.Match(i, "UNIQUESTRING"))
-
//{
-
// MessageBox.Show("File with regex is : " + i);
-
//}
-
-
//string ex = null;
-
//ex = (from line in File.ReadAllLines(i)
-
// where line == "UNIQUESTRING"
-
// select line).ToString();
-
-
}
-
| 
July 5th, 2009, 04:17 PM
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,690
Provided Answers: 3 | | | re: Search text files for string
Are you actually looking for the word "UNIQUESTRING" in all upper case?
Or is UNIQUESTRING meant to be a variable thus shouldn't have quotes around it?
.Contains is case-sensitive so could that be the issue? You are searching for upper case in a lower case file?
| 
July 5th, 2009, 04:20 PM
| | Newbie | | Join Date: Aug 2008
Posts: 7
| | | re: Search text files for string
Yes, the string would be in all uppercase. The contents of the text files are also in uppercase.
| 
July 5th, 2009, 04:25 PM
| | Newbie | | Join Date: Aug 2008
Posts: 7
| | | re: Search text files for string
I should probably add that the snippet of code does not compile as i'm having some more type problems.
| 
July 6th, 2009, 12:25 AM
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,690
Provided Answers: 3 | | | re: Search text files for string
Well... yeah... Not compiling will keeping it from finding what you are searching for.
| 
July 6th, 2009, 04:25 PM
|  | Forum Leader | | Join Date: Apr 2008 Location: San Antonio, TX (USA)
Posts: 2,569
| | | re: Search text files for string
This should help: - string path = @"c:\dev\somedir";
-
string searchtext = "something";
-
DirectoryInfo di = new DirectoryInfo(path);
-
FileInfo[] files = di.GetFiles("*.txt");
-
foreach (FileInfo file in files)
-
{
-
using (StreamReader sr = new StreamReader(file.FullName))
-
{
-
string content = sr.ReadToEnd().ToLower();
-
if(content.Contains(searchtext.ToLower()))
-
Console.WriteLine("{0} contains \"{1}\"",file.Name,searchtext);
-
else
-
Console.WriteLine("{0} does not contain \"{1}\"", file.Name, searchtext);
-
}
-
}
This is written for a console app, so you will have to make appropriate changes.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|