Opt In (Do Not Edit Here)

Dec 19, 2012

TextSearch (Release 1.0) --- From Multiple Text Documents

The TextSearch-1.0 reveals the track to enhance the search functionality of Text Documents(s) belongs to same path/folder.

TextSearch-1.0, allows user to search a specific word from many text documents (File Format: *.txt) that belongs to same directory, with no need to open individual file(s) to search a word/text. It has simple GUI.

About Classes used -

#1: FolderBrowserDialog class allows user to select a folder.
#2: Directory class allows user to enumerate the file(s) available at specified directory path.
#3: StreamReader class provides an access to read the data from Stream such as Text File.
#4: Process class, to allow user to start a specific process.

Namespace Required         -           System.IO,

Controls Used           -          
            1. TextBox Control (txtFolder, txtSearch)
          2. ListBox Control (lbFiles)
          2. Button Control (btnFolder, btnSearch, btnReset)
Here I present a Code for finding a text file(s) with a specified word/text provided as search string.

The Code:
1. Variable Declarations
            String target = ""; // Target string to Search
            String path = "";   // Path of Directory
            string[] files;     // Store file name(s) from given path
            bool search;        // Determine whether Target string present or NOT
            string[] list;      // Store file name(s) which includes Target string

Listing 1

2. List All Text File(s) (File Format: *.txt)
      void getFiles()
                  lbFiles.Items.Clear();    //Clears the Previous file list (if any)
                  path = txtFolder.Text;    //Current path of selected folder
              if (path != ""){

                  // Collects the name of files with extension of ".txt"
                  files = Directory.GetFiles(@path, "*.txt");
                  foreach (string item in files)  //Add File Name to ListBox

                  //Display Total No. of Files
                  lblTotal.Text = "Total Files: " + lbFiles.Items.Count.ToString(); }
Listing 2

3. After Click the “Folder” button, assign the selected path from to txtFolder TextBox, & call the getFiles() method to list all files within that folder.

4. List All Text File(s) which includes a string to be search.

      void Search()
            int j = 0;        // to set the index of “list” array
            target = txtSearch.Text;
            list = new string[lbFiles.Items.Count];

            for (int i = 0; i < lbFiles.Items.Count; i++)
                  search = false;

                  //Check whether Target String available within List of Text File(s)
                  if (!((new StreamReader(path + "\\"
                                    + lbFiles.Items[i]).ReadToEnd()).Contains(target)))
                  { search = true; }

                  list.SetValue(search.ToString(), j);

            //Delete File(s) from Search (if Search Not Found)
            for (int i = list.Length - 1; i >= 0; i--)
                  if (list.GetValue(i).ToString() != "False")

Listing 3

5. Call a Search() method to list a resultant text file(s) at an OnClick Event of “Search” button.

6. Now execute the Application & see the intended result (Figure 1 & 2).
Intended Result:

Figure 1: After Select a Folder

Figure 2: After specifying a string to search

Additional Functionality:

  •  TextSearch-1.0, allow to regain a list of initial text files as before search (“Reset Search”).
  •   User can open a file, by double clicking the file name.

          The Program TextSearch-1.0 is written is Visual C#, which allows a user to search a text file(s) which includes a specified word/text.

About The Author :

Freelancer and IT Engineer
Softwares Developed by Mayur Dighe ImmortalDotNet.WordPress.com


All Rights Reserved. 2014 Copyright SIMPLITONA

Powered By Blogger | Published By Gooyaabi Templates Designed By : BloggerMotion