How to search text files for string? | Bytes (2024)

tig2810

How to search text files for string? | Bytes (1) 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. stringdirectory=@"D:\SearchFolder";
  2. DirectoryInfodirinfo=newDirectoryInfo(directory);
  3. FileInfo[]files=dirinfo.GetFiles("EE*.PRN");
  4. foreach(FileInfoiinfiles)
  5. {
  6. List<string>lines=File.ReadAllLines(i.ToString().ToList());
  7. foreach(stringlineinlines)
  8. {
  9. if(line.Contains("UNIQUESTRING"))
  10. textBox2.Text+=i+"contains"+line.ToString()+Environment.NewLine;
  11. }
  12. //Regex.Match(i,"UNIQUESTRING");
  13. //if(Regex.Match(i,"UNIQUESTRING"))
  14. //{
  15. //MessageBox.Show("Filewithregexis:"+i);
  16. //}
  17. //stringex=null;
  18. //ex=(fromlineinFile.ReadAllLines(i)
  19. //whereline=="UNIQUESTRING"
  20. //selectline).ToString();
  21. }

Jul 5 '09 #1

Subscribe Reply

6 How to search text files for string? | Bytes (2) 21887 How to search text files for string? | Bytes (3)

tlhintoq

3,525 How to search text files for string? | Bytes (5) 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 How to search text files for string? | Bytes (6) 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 How to search text files for string? | Bytes (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 How to search text files for string? | Bytes (9) 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 How to search text files for string? | Bytes (11) Recognized Expert Specialist

This should help:

Expand|Select|Wrap|Line Numbers

  1. stringpath=@"c:\dev\somedir";
  2. stringsearchtext="something";
  3. DirectoryInfodi=newDirectoryInfo(path);
  4. FileInfo[]files=di.GetFiles("*.txt");
  5. foreach(FileInfofileinfiles)
  6. {
  7. using(StreamReadersr=newStreamReader(file.FullName))
  8. {
  9. stringcontent=sr.ReadToEnd().ToLower();
  10. if(content.Contains(searchtext.ToLower()))
  11. Console.WriteLine("{0}contains\"{1}\"",file.Name,searchtext);
  12. else
  13. Console.WriteLine("{0}doesnotcontain\"{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

reply

nomibashir

1 How to search text files for string? | Bytes (12) 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. DirectoryInfodir=newDirectoryInfo(@"d:\TextFiles\");//dirwhereyouwanttusearchyourfile
  2. FileInfo[]fileInfo=dir.GetFiles();
  3. foreach(FileInfofileinfileInfo)
  4. {
  5. StreamReadersr=newStreamReader(file.FullName);
  6. stringallRead=sr.ReadToEnd();
  7. sr.Close();
  8. stringfileName=file.Name;
  9. if(Regex.IsMatch(allRead,"UNIQUESTRING"))
  10. {
  11. MessageBox.Show("Found:"+fileName);
  12. break;
  13. }
  14. else
  15. {
  16. MessageBox.Show("NotFound");
  17. }
  18. }

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 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...

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

Couldn’t 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

How to search text files for string? | Bytes (2024)

FAQs

How can we search for strings in files? ›

Use the grep command to search the specified file for the pattern specified by the Pattern parameter and writes each matching line to standard output. This displays all lines in the pgm. s file that begin with a letter.

How to search text in multiple text files? ›

Let's look at the top 5 options for searching text in multiple Word documents.
  1. SeekFast. The easiest and most convenient tool for searching text in multiple Word files is SeekFast. ...
  2. File Explorer. File Explorer is the built-in Windows program for working with files. ...
  3. PowerGrep. ...
  4. Agent Ransack. ...
  5. DocFetcher. ...
  6. Conclusion.
Jan 28, 2021

How to find all files containing a specific text string on Windows? ›

Windows Search Content
  1. Type “Indexing options” in the Windows Search Bar and select Indexing Options from the results.
  2. From the Indexing Options window, click Advanced at the bottom.
  3. Under Advanced Options, click on the File types tab. ...
  4. Check the option Indexed Properties and File contents option and click OK to confirm.

How do I search for a string in all text files in a folder? ›

  1. Method 1 of 2:Searching for Documents Containing Certain Text. Press ⊞ Win+S. ...
  2. Type indexing options. A list of matching results will appear.
  3. Click Indexing Options. ...
  4. Click Advanced. ...
  5. Click the File Types tab. ...
  6. Select Index Properties and File Contents. ...
  7. Click OK. ...
  8. Press ⊞ Win+S.
Aug 12, 2022

How do I search for a string in a list of files? ›

You should learn Data Structures & Algorithms. Now, we will use the grep command with the -l option to search for text in given files located inside the current directory. See the following example: It can be clearly said from the above example that the grep command has successfully found the given string in file1.

How do I search for a specific string? ›

The search() method searches a string for a string (or a regular expression) and returns the position of the match:
  1. Examples. let text = "Please locate where 'locate' occurs!"; ...
  2. Examples. Perform a search for "ain": ...
  3. Examples. Check if a string includes "world": ...
  4. Examples. Returns true: ...
  5. Returns false: ...
  6. Examples.

How do I search text contents of files? ›

Search file contents
  1. In Windows 10, click on "Advanced options" -> "File Contents."
  2. In Windows 11, type the search words and click on the "Search options" -> "File Contents."
Aug 21, 2024

How to find strings in multiple files in Windows? ›

To search for multiple strings in a set of files, you must create a text file that contains each search criterion on a separate line. Use spaces to separate multiple search strings unless the argument is prefixed with /c.

How do I search for a string in multiple word documents? ›

Click on “Search.” Put a tick mark against “Always search file names and content.” Now, Windows will show the content of those folders you have selected and their sub-folders. As soon as the indexing stops, you can search words in the upper right corner of the File Explorer.

How do I search all TXT files? ›

Enter the following command line: ls *. txt.

How do I find all files that contain two strings? ›

To search the current directory for all files that contain two independent strings, you can use the following terminal command: grep -lr "common" $(grep -lr "rare" .)

What command would you use to find a string in all files? ›

To find all files containing a specific text (string) on a Linux system, you can use the grep command. grep is a powerful command-line utility that searches for patterns within files.

How do I find a string in a text file? ›

Finding the index of the string in the text file using readline() In this method, we are using the readline() function, and checking with the find() function, this method returns -1 if the value is not found and if found it returns 0.

How do I find a file that contains a string? ›

How to find files which contains defined text in Linux and...
  1. apt-get update && apt install grep. Command to install in RedHat-based OS (like Centos) is:
  2. yum install grep. To find all files with required string just run:
  3. grep -r <looked_for_text> <dir>
Feb 2, 2023

How do I search for a specific string in File Explorer? ›

How to Search for a Specific Phrase in Windows 10 File Explorer
  1. Open Windows Explorer.
  2. Type the following string in the search box: content:”your phrase” ...
  3. You will see the colour of the text changes to light blue – I assume this means Windows recognises this as a specific instruction.
Aug 18, 2018

How to search keywords in files? ›

Search file contents

To search the text of the documents: In Windows 10, click on "Advanced options" -> "File Contents." In Windows 11, type the search words and click on the "Search options" -> "File Contents."

Which command will search for a text string in a file? ›

To search for a text string in a file, you can use the grep command. grep is a powerful command-line tool used for searching text patterns within files. Syntax : grep "text string" file_name.

How do you check if a string is present in a file? ›

  1. if you use a text editor, you can use the “Find” function in the editor menu interface or with keyboard commands, but if you want to do it programmatically, write a script to read the file and compare strings.
  2. If the string is contained in a single line, a read line by line is OK.
Feb 19, 2022

References

Top Articles
Bill Gates Net Worth
What is Bill Gates' net worth?
Camping World Of New River
Barstool Sports Gif
Craigslist The Big Island
Pobierz Papa's Mocharia To Go! na PC za pomocą MEmu
Rickrolling Link Generator
Quadrilateral Angles Sum Property - Theorem and Proof
Survivor Australia Wiki
Futuretechgirls Contact
Cold War Brainpop Answers
Wlds Obits
Hudson River Regional Conference Inc. · 112-14 107th ave., South Richmond Hill, NY 11419
Craigslist Rooms For Rent North County San Diego
Family Guy Wiki Peter
Sauce 423405
Westelm Order
Icl Urban Dictionary
Wayne State Dean's List
55000 Pennies To Dollars
Storm Prediction Center Convective Outlook
Experience the Convenience of Po Box 790010 St Louis Mo
Tyrone's Unblocked Games Basketball
Elfqrindiscard
How to Get Into UCLA Medical School: Requirements and Strategies — Shemmassian Academic Consulting
Jeff Danker Net Worth
Logisch werving en selectie B.V. zoekt een Supply Chain &amp; Logistics Engineer in Coevorden | LinkedIn
27 Sage Street Holmdel Nj
Mark Rosen announces his departure from WCCO-TV after 50-year career
Palmer Santin Funeral Home Fullerton Nebraska Obituaries
Coil Cleaning Lititz
Did Hannah Jewell Leave Wnem Tv5
Unblocked Games 66E
Hmnu Stocktwits
Blue Beetle Showtimes Near Regal Independence Plaza & Rpx
Hingham Police Scanner Wicked Local
Star Wars Galaxy Of Heroes Webstore
Z93 Local News Monticello Ky
Beaufort Mugfaces Last 72 Hours
Mcoc Black Panther
Experity Installer
Craigs List Outdoor Furniture
How Much Does Costco Gas Cost Today? Snapshot of Prices Across the U.S. | CostContessa
Busted Newspaper Mcpherson Kansas
Alvin Isd Ixl
Trinity Portal Minot Nd
Olive Onyx Amora
Myrtle Beach Pelicans Stadium Seating Chart
SF bay area cars & trucks "chevrolet 50" - craigslist
Ds Cuts Saugus
Omni Id Portal Waconia
Four Observations from Germany’s barnstorming 5-0 victory over Hungary
Latest Posts
Article information

Author: Jerrold Considine

Last Updated:

Views: 6035

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.