Connecting Tech Pros Worldwide Forums | Help | Site Map

Search text files for string

Newbie
 
Join Date: Aug 2008
Posts: 7
#1: Jul 5 '09
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


Expand|Select|Wrap|Line Numbers
  1.  
  2.             string directory = @"D:\SearchFolder";
  3.             DirectoryInfo dirinfo = new DirectoryInfo(directory);
  4.  
  5.             FileInfo[] files = dirinfo.GetFiles("EE*.PRN");
  6.  
  7.             foreach (FileInfo i in files)
  8.             {                
  9.                 List<string> lines = File.ReadAllLines(i.ToString().ToList());
  10.                 foreach (string line in lines)
  11.                 {
  12.                     if (line.Contains("UNIQUESTRING"))
  13.                         textBox2.Text += i + "contains" + line.ToString() + Environment.NewLine;
  14.                 }
  15.  
  16.                 //Regex.Match(i, "UNIQUESTRING");
  17.                 //if (Regex.Match(i, "UNIQUESTRING")) 
  18.                 //{
  19.                 //    MessageBox.Show("File with regex is : " + i);
  20.                 //}
  21.  
  22.                 //string ex = null;
  23.                 //ex = (from line in File.ReadAllLines(i)
  24.                 //      where line == "UNIQUESTRING"
  25.                 //      select line).ToString();
  26.  
  27.         }
  28.  

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 2,305
#2: Jul 5 '09

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?
Newbie
 
Join Date: Aug 2008
Posts: 7
#3: Jul 5 '09

re: Search text files for string


Yes, the string would be in all uppercase. The contents of the text files are also in uppercase.
Newbie
 
Join Date: Aug 2008
Posts: 7
#4: Jul 5 '09

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.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 2,305
#5: Jul 6 '09

re: Search text files for string


Well... yeah... Not compiling will keeping it from finding what you are searching for.
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,695
#6: Jul 6 '09

re: Search text files for string


This should help:
Expand|Select|Wrap|Line Numbers
  1. string path = @"c:\dev\somedir";
  2. string searchtext = "something";
  3. DirectoryInfo di = new DirectoryInfo(path);
  4. FileInfo[] files = di.GetFiles("*.txt");
  5. foreach (FileInfo file in files)
  6. {
  7.     using (StreamReader sr = new StreamReader(file.FullName))
  8.     {
  9.         string content = sr.ReadToEnd().ToLower();
  10.         if(content.Contains(searchtext.ToLower()))
  11.             Console.WriteLine("{0} contains \"{1}\"",file.Name,searchtext);
  12.         else
  13.             Console.WriteLine("{0} does not contain \"{1}\"", file.Name, searchtext);
  14.     }
  15. }
This is written for a console app, so you will have to make appropriate changes.
Reply