473,654 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to search text files for string?

7 New Member
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 21814
tlhintoq
3,525 Recognized Expert Specialist
Are you actually looking for the word "UNIQUESTRI NG" 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
tig2810
7 New Member
Yes, the string would be in all uppercase. The contents of the text files are also in uppercase.
Jul 5 '09 #3
tig2810
7 New Member
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 Recognized Expert Specialist
Well... yeah... Not compiling will keeping it from finding what you are searching for.
Jul 5 '09 #5
Curtis Rutland
3,256 Recognized Expert Specialist
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
nomibashir
1 New Member
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
1683
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 between the ASP delimiters <% %> How can I search for text strings in ASP a folder full of ASP files with out opening each one individually and searching? thanks for any help.
5
9166
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 Explorer, right click and 'search for word in file' ... it reports back that the text string was not found. I've fooled around with the 'advanced' settings but can't seem to make it work... even when I'm searching a single folder with a single...
1
3050
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 application which will perform "searches" for text values in a directory of html files, trying to match only the non-tagged text in the documents.
60
49059
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 be indexed/sorted. I don't want to load the entire file into physical memory, memory-mapped files are ok (and preferred). Speed/performance is a requirement -- the target is to locate the string in 10 seconds or less for a 100 MB file. The...
1
2715
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 Me.txtEmail.SetFocus Exit Sub End If Me.txtStatusBar.Value = "Parsing..." strEmail = Me.txtEmail.Value
16
2840
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 help with one part of the code. When a button is clicked I need to have it go out to a network drive location and count how many files are present with a certain file extension. Then store that number in a declared variable. Is this possible? Can...
2
1966
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 i can search.like y:\\serverfolde\\folder am tring to make an exe. different people will map to different drive. so i cant give like that.should give like @\\server\\serverfolde\\folder\\ but it is not working. My code is<code>...
2
1677
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 i can search.like y:\\serverfolde\\folder am tring to make an exe. different people will map to different drive. so i cant give like that.should give like @\\server\\serverfolde\\folder\\ but it is not working. My code is...
3
3545
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 other Windows flavors. Procedure: 1. Create a simple text file named test.txt. 2. Open the text file in a text editor and add a simple test word such as "blah" (not quotes).
0
10751
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 inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
8708
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.