473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to search text files for string?

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.  
Jul 5 '09 #1
6 21750
tlhintoq
3,525 Expert 2GB
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?
Jul 5 '09 #2
Yes, the string would be in all uppercase. The contents of the text files are also in uppercase.
Jul 5 '09 #3
I should probably add that the snippet of code does not compile as i'm having some more type problems.
Jul 5 '09 #4
tlhintoq
3,525 Expert 2GB
Well... yeah... Not compiling will keeping it from finding what you are searching for.
Jul 5 '09 #5
Curtis Rutland
3,256 Expert 2GB
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.
Jul 6 '09 #6
This code is developed in C# and working wel. I have not much time so i did not finished in well maners. You can use this code to search your file according to your Question .

Expand|Select|Wrap|Line Numbers
  1. DirectoryInfo dir = new DirectoryInfo(@"d:\Text Files\"); // dir where you want tu search your file
  2.             FileInfo[] fileInfo = dir.GetFiles();
  3.  
  4.             foreach (FileInfo file in fileInfo)
  5.             {                
  6.                 StreamReader sr = new StreamReader(file.FullName);
  7.                 string allRead = sr.ReadToEnd();
  8.                 sr.Close();
  9.                 string fileName = file.Name;
  10.                 if (Regex.IsMatch(allRead, "UNIQUESTRING"))
  11.                 {
  12.                     MessageBox.Show("Found: "+ fileName);
  13.                     break;
  14.                 }
  15.                 else
  16.                 {
  17.                     MessageBox.Show("Not Found");
  18.                 }
  19.             }
Feb 28 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: tmb | last post by:
I'm trying to use the Microsoft Search to search for a text string in a folder full of a bunch of ASP files. Seems like the normal "Search for text in files" program in XP Pro won't search...
5
by: tmb | last post by:
I need to search a folder & sub-folders for key words in ASP files... I can open the files with Notepad and see the text string there... But when I try to navigate to the folder with Windows...
1
by: Dave Townsend | last post by:
Hi, Can anybody help me with the following piece of code? The purpose behind the code is to parse HTML files, strip out the tags and return the text between tags. This is part of a larger...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
1
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
16
by: Computer geek | last post by:
Hello, I am new to VB.NET and programming in general. I have taught myself a lot of the basics with vb.net but am still quite the novice. I am working on a little application now and I need some...
2
by: princymg | last post by:
I want to search a file from server and want to copy it to the local disk. how it is done? This is working if the file is in my hard disk itself.But not when it comes to server. If i map the server...
2
by: princymg | last post by:
I want to search a file from server and want to copy it to the local disk. how it is done? This is working if the file is in my hard disk itself.But not when it comes to server. If i map the server...
3
by: =?Utf-8?B?UGVycmlud29sZg==?= | last post by:
Not sure where to post this... Found some interesting behavior in Windows Search (Start =Search =All files and folders =search for "A word or phrase in the file:"). This applies to XP and maybe...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.