How to search a keyword in multiple files? and copy them to other files using C# RRS feed

  • Question

  • I'm stuck on how i gonna search a certain keyword in Multiple text file and want to copy them to new text files...Furthermore, if the keyword that i want to search is found (maybe in a file contain one or more keyword that i want to search), i want to copy 2 line above and 2 line below the keyword.

    So, at the new file should contain from which file it copy from, the whole line of the keyword +2line above and below the keyword.

    Please help me out,im a newbies in the programming world but been ask to do very challenging task. If my problem does not clear, please let me know.

    Thanks in advance...

    Thursday, November 4, 2010 6:43 AM

All replies

  • For example,

    1. I set a directory (eg. dekstop).

    2. Search in to that directory with only certain extension (eg *.txt).

    2.1. Or i select multiple of files ( eg select 10 text files).

    3. Then I want to search a keyword (eg. warning)

    4. Then the code should be able to search all the file (.txt) in the directory (desktop) with contains the keyword (warning).

    4.1. Or search all files (10 text files) with contains the keyword (warning).

    5. dump all the found item to new.txt that contain:

        a)from where it copy from.

        b)all the line that contains the keyword plus 2 line above and below it.


    Hope it clear.Thanks in advance...please help the newbies out.

    Thursday, November 4, 2010 9:11 AM
  • Hi,

    You can try that follows:

     private void search(String key, String[] filenames)
          List<String> list = new List<string>();
          for (int i = 0; i < filenames.Length; i++)
            list.Add("$$");// seperate every files
          FileStream fs = File.Create("new.txt");
          StreamWriter sw = new StreamWriter(fs);
          for (int i = 0; i < list.Count; i++)
            if (!list[i].Equals("$$") && key.Equals(list[i]) )
              for (int j = ((i - 2) > 0 ? i - 2 : 0); j <= i + 2 && j < list.Count; j++)
                if (!list[j].Equals("$$"))

    I have test with "search("asdf", new String[]{ "aa.txt","bb.txt" });". And it works well. Hope helps.

    Vin Jin

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Friday, November 5, 2010 2:12 AM
  • Hi, sory for late reply, i had been emitted to hospital...

    1) i just want to ask on how do i separate every file that i want to search???

    2)Can i put a directory example desktop at list.Add

    3)Is the new.txt show from where the keyword is copy from???

    thanks in advance...

    Wednesday, November 10, 2010 1:06 AM
  • Hi, i also had a need for that once. I made a solution for that. please take the look at it.

    1. Adding Multiple Keywords (From sqlite db)

    2. Recursive file search (look into subdirectories)

    3. View the search output

    4. send the output to clipboard as well as to new text file.

    Hope it helps you. You can contact me on further..

    Monday, December 9, 2019 8:02 AM