tig2810
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
- stringdirectory=@"D:\SearchFolder";
- DirectoryInfodirinfo=newDirectoryInfo(directory);
- FileInfo[]files=dirinfo.GetFiles("EE*.PRN");
- foreach(FileInfoiinfiles)
- {
- List<string>lines=File.ReadAllLines(i.ToString().ToList());
- foreach(stringlineinlines)
- {
- if(line.Contains("UNIQUESTRING"))
- textBox2.Text+=i+"contains"+line.ToString()+Environment.NewLine;
- }
- //Regex.Match(i,"UNIQUESTRING");
- //if(Regex.Match(i,"UNIQUESTRING"))
- //{
- //MessageBox.Show("Filewithregexis:"+i);
- //}
- //stringex=null;
- //ex=(fromlineinFile.ReadAllLines(i)
- //whereline=="UNIQUESTRING"
- //selectline).ToString();
- }
Jul 5 '09 #1
Subscribe Reply
6 21887
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
reply
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
reply
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
reply
tlhintoq 3,525 Recognized Expert Specialist
Well... yeah... Not compiling will keeping it from finding what you are searching for.
Jul 5 '09 #5
reply
Curtis Rutland 3,256 Recognized Expert Specialist
This should help:
Expand|Select|Wrap|Line Numbers
- stringpath=@"c:\dev\somedir";
- stringsearchtext="something";
- DirectoryInfodi=newDirectoryInfo(path);
- FileInfo[]files=di.GetFiles("*.txt");
- foreach(FileInfofileinfiles)
- {
- using(StreamReadersr=newStreamReader(file.FullName))
- {
- stringcontent=sr.ReadToEnd().ToLower();
- if(content.Contains(searchtext.ToLower()))
- Console.WriteLine("{0}contains\"{1}\"",file.Name,searchtext);
- else
- Console.WriteLine("{0}doesnotcontain\"{1}\"",file.Name,searchtext);
- }
- }
This is written for a console app, so you will have to make appropriate changes.
Jul 6 '09 #6
reply
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
- DirectoryInfodir=newDirectoryInfo(@"d:\TextFiles\");//dirwhereyouwanttusearchyourfile
- FileInfo[]fileInfo=dir.GetFiles();
- foreach(FileInfofileinfileInfo)
- {
- StreamReadersr=newStreamReader(file.FullName);
- stringallRead=sr.ReadToEnd();
- sr.Close();
- stringfileName=file.Name;
- if(Regex.IsMatch(allRead,"UNIQUESTRING"))
- {
- MessageBox.Show("Found:"+fileName);
- break;
- }
- else
- {
- MessageBox.Show("NotFound");
- }
- }
Feb 28 '11 #7
reply
Sign in to post your reply or Sign up for a free account.
Similar topics
2 1696 XP's search for string in files won't search between <% %> 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. ASP / Active Server Pages |
5 9200 How to search ASP files for text string with Windows Explorer Search ?? 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... ASP / Active Server Pages |
1 3073 std::search question... 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. C / C++ |
60 49268 Fastest way to search text file for string 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... C# / C Sharp |
1 2734 Search pattern 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 Microsoft Access / VBA |
16 2876 vb.net search network drive 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... Visual Basic .NET |
2 1983 To search a file and copy it to server 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>... General |
2 1700 To search a file and to copy it from server 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... .NET Framework |
3 3571 Text in ASP Scripts Hidden From Windows Search 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). ASP / Active Server Pages |
10812
Play with image A Technique in Java - Search image, Convert image to text , Hide data 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... Java |
11521
Problem With Comparison Operator <=> in G++ by: Oralloy |last post by: Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in... C / C++ |
11104
Maximizing Business Potential: The Nexus of Website Design and Digital Marketing 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... Online Marketing |
9854
AI Job Threat for Devs 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 projectplanning, coding, testing, and deploymentwithout 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... Career Advice |
1 8214
Access Europe - Using VBA to create a class based on a table - Wed 1 May 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... Microsoft Access / VBA |
7378 Couldnt get equations in html when convert word .docx file to html file in C#. 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();... C# / C Sharp |
6072 Trying to create a lan-to-lan vpn between two differents networks 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... Networking - Hardware / Configuration |
6291 Windows Forms - .Net 8.0 by: adsilva |last post by: A Windows Forms form does not have the event Unload, like VB6. What one acts like? Visual Basic .NET |
1 4901 transfer the data from one system to another through ip address 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 C# / C Sharp |
3 3497
Comprehensive Guide to Website Development in Toronto: Expert Insights from BSMN Consultancy 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... General |