Connecting Tech Pros Worldwide Help | Site Map

Search text files for string

  #1  
Old 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


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.  
  #2  
Old July 5th, 2009, 04:17 PM
tlhintoq's Avatar
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?
  #3  
Old 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.
  #4  
Old 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.
  #5  
Old July 6th, 2009, 12:25 AM
tlhintoq's Avatar
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.
  #6  
Old July 6th, 2009, 04:25 PM
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,569

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search open files for string and then copy that row into a new workbook taylordude answers 2 March 21st, 2008 09:18 PM
Fastest way to search text file for string Julie answers 60 November 16th, 2005 12:21 PM
How to search ASP files for text string with Windows Explorer Search ?? tmb answers 5 July 22nd, 2005 01:49 AM