Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8468769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:01:48+00:00 2026-06-10T16:01:48+00:00

I am trying to search for a particular occurrence of a string in some

  • 0

I am trying to search for a particular occurrence of a string in some files belonging to a directory. (The search is also performed in the sub directories. Currently, I came up with a solution something like this.

  1. Get all filenames inside a directory and its sub directories.
  2. Open files one by one.
  3. Search for a particular string
  4. If it contains, store filename in an array.
  5. Continue this till the last file.

    string[] fileNames = Directory.GetFiles(@"d:\test", "*.txt", SearchOption.AllDirectories);
    foreach (string sTem in fileNames)
    {
        foreach (string line in File.ReadAllLines(sTem))
        {
            if (line.Contains(SearchString))
            {
                MessageBox.Show("Found search string!");
                break;
            }
        }
    }
    

I think there can be other methods/approach efficient and speeder than this? Using a batch file? OK. Another, solution is to use findstr (but how to use it directly with C# program without a batch file ? What is the most efficient (or more efficient than what I did?)
Code examples are much appreciated!

Found out another solution.

Process myproc = new Process();
myproc.StartInfo.FileName = "findstr";
myproc.StartInfo.Arguments = "/m /s /d:\"c:\\REQs\" \"madhuresh\" *.req";
myproc.StartInfo.RedirectStandardOutput = true;
myproc.StartInfo.UseShellExecute = false;


myproc.Start();
string output = myproc.StandardOutput.ReadToEnd();
myproc.WaitForExit();

Is this execution of a process good ? Comments on this too are welcome!

According to the @AbitChev’s method, a sleek (I don’t know if it’s efficient!). Anyways, it goes on like this. This one searches all the directory as well as the subdirectories!

IEnumerable<string> s = from file in Directory.EnumerateFiles("c:\\directorypath", "*.req", SearchOption.AllDirectories)
                   from str in File.ReadLines(file)
                   //where str.Contains("Text@tosearched2")
                   where str.IndexOf(sSearchItem, StringComparison.OrdinalIgnoreCase) >= 0
                   select file;

        foreach (string sa in s)
            MessageBox.Show(sa);

(for case-insensitive search. Maybe that could help someone.)
Please comment! Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:01:51+00:00Added an answer on June 10, 2026 at 4:01 pm

    How about somthing like this

    var found = false;
    string file;
    
    foreach (file in Directory.EnumerateFiles(
                "d:\\tes\\",
                "*.txt",
                SearchOption.AllDirectories))
    {
        foreach(var line in File.ReadLines(file))
        {
            if (line.Contains(searchString))
            {
                found = ture;
                break;
            }
        }
    
        if (found)
        {
                break;
        }
    }
    
    if (found)
    {
        var message = string.Format("Search string found in \"{0}\".", file)
        MessageBox.Show(file);
    }
    

    This has the advantage of loading only what is required into memory, rather than the names of all the files then, the contents of each file.


    I note you are using String.Contains which

    performs an ordinal (case-sensitive and culture-insensitive) comparison

    This would allow us to do a simple charachter wise compare.

    I’d start with a little helper function

    private static bool CompareCharBuffers(
        char[] buffer,
        int headPosition,
        char[] stringChars)
    {
        // null checking and length comparison ommitted
    
        var same = true;
        var bufferPos = headPosition;
        for (var i = 0; i < stringChars.Length; i++)
        {
            if (!stringChars[i].Equals(buffer[bufferPos]))
            {
                same = false;
                break;
            }
    
            bufferPos = ++bufferPos % (buffer.Length - 1);
        }
    
        return same;
    }
    

    Then I’d alter the previous algorithm to use the function like this.

    var stringChars = searchString.ToCharArray();
    var found = false;
    string file;
    
    
    foreach (file in Directory.EnumerateFiles(
                "d:\\tes\\",
                "*.txt",
                SearchOption.AllDirectories))
    {
        using (var reader = File.OpenText(file))
        {
            var buffer = new char[stringChars.Length];
            if (reader.ReadBlock(buffer, 0, buffer.Length - 1) 
                    < stringChars.Length - 1)
            {
                continue;
            }
    
            var head = 0;
            var nextPos = buffer.Length - 1;
            var nextChar = reader.Read();
            while (nextChar != -1)
            {
                buffer[nextPos] = (char)nextChar;
    
                if (CompareCharBuffers(buffer, head, stringChars))
                {
                   found = ture;
                   break;
                }
    
                head = ++head % (buffer.Length - 1);
                if (head == 0)
                {
                    nextPos = buffer.Length - 1;
                }
                else
                {
                    nextPos = head - 1;
                } 
    
                nextChar = reader.Read();
            }
    
            if (found)
            {
                break;
            }
        }
    }
    
    if (found)
    {
        var message = string.Format("Search string found in \"{0}\".", file)
        MessageBox.Show(file);
    }
    

    this holds only as many chars as the search string contains in memory and uses rolling buffer across each file. Theoretically the file could contain no new lines and consume your whole disk, or, your search string could contain a new line.


    As further work I’d convert the per file part of the algorithm into a function and investigate a multi-threaded approach.

    So this would be the internal function,

    static bool FileContains(string file, char[] stringChars)
    {
        using (var reader = File.OpenText(file))
        {
            var buffer = new char[stringChars.Length];
            if (reader.ReadBlock(buffer, 0, buffer.Length - 1) 
                    < stringChars.Length - 1)
            {
                return false;
            }
    
            var head = 0;
            var nextPos = buffer.Length - 1;
            var nextChar = reader.Read();
            while (nextChar != -1)
            {
                buffer[nextPos] = (char)nextChar;
    
                if (CompareCharBuffers(buffer, head, stringChars))
                {
                   return true;
                }
    
                head = ++head % (buffer.Length - 1);
                if (head == 0)
                {
                    nextPos = buffer.Length - 1;
                }
                else
                {
                    nextPos = head - 1;
                } 
    
                nextChar = reader.Read();
            }
    
            return false;
        }
    }
    

    Then you could process the files in parallel like this

    var stringChars = searchString.ToCharArray();
    
    if (Directory.EnumerateFiles(
                "d:\\tes\\",
                "*.txt",
                SearchOption.AllDirectories)
        .AsParallel()
        .Any(file => FileContains(file, stringChars)))
    {
        MessageBox.Show("Found search string!");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just trying to search a particular text inside files belonging to a directory(and all
I am trying to download google images for a particular search. Currently, if i
Hello I am trying to search particular string in file and want to replace
I am trying to search a particular string in all sheet names of a
I am trying to search for a particular string ERROR in all the worksheets
I'm trying to return a particular line from files found from this search: find
I am trying to search for files which matches following expression with sed [a-zA-Z0-9]{1,10}\s{1,5}\
I'm trying to search through a Word document and replace a specific string (Using
I am trying to search for multiple strings within a larger string and if
My data[l][m] contains 1,2,3,4,5 I'm trying to search for a particular number, say '2'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.