Never underestimate the power of Passion!

Thursday, 30 March 2017

On 00:20 by Vardan Kumar in    No comments
No. of Hits in C#

How to get all the file names which contains a particular word

Analysis is the most important step which every software development life cycle requires to avoid rework. Sometimes it becomes a tedious task and could eat up a lot of time hence increasing cost of software development. For example: Some code changes in the current system needs to be implemented, changes like you have to change a particular identifier name to another in the entire system. So if this process is needed to be carried out professionally then the impact analysis of that identifier should be captured. So what would you open each and every source file, header files or what ever your system contains and try to capture, how many times the identifier hits in a particular file, well reading thousands of files having thousands of line definitely not feasible. So instead of manually doing this task, automation should be implemented for the same.

So let us make c# windows form application which will take directory as well as identifier name from the user and returns a txt file or excel file or what ever format you want and get all the file names along with the number of hits of a word in a particular file.

Prerequisites



Logic

  • Let the user enter the identifier name and select the directory.
    • Validations should be applied i.e if user leaves the field blank or do not select a directory.
    •  if (String.IsNullOrEmpty(textBox1.Text))
                  {
                      MessageBox.Show("Please enter identifier name");
                      return;
                  }
  • We'll use a list<string> to add as one complete row to data table which in turn will become one row of excel.
    • The contents of <list string> will be in sequence, first the name of file and second the no. of hits corresponding to that file.
    • The list will be added as row to data table.
    • List will be cleared so as to populate a row for next file in data table.
  • Add columns to data table giving your desired names. Also check if data table already contains columns, what if a user in a single run search and retrieve more than once.
    •  if(dt.Columns.Count==0)
                 {
                  dt.Columns.Add("Programs referencing SAK");
                  dt.Columns.Add("Total No. of Hits for SAK");
                  }
  • Now for each file in the selected directory enabling search option to search all directories and specifying desired file format(* to search all file formats).
    • foreach (string file in Directory.EnumerateFiles(dir, "*", SearchOption.AllDirectories).
    • Copy all the contents of the file at once in a string.
    • string contents = File.ReadAllText(file);
  • Check if the entered identifier name exist in that file.
    •  if (contents.IndexOf(textBox1.Text, StringComparison.OrdinalIgnoreCase) >= 0
      • Use string extension to ignore case.
        •  public static class StringExtensions
              {
                  public static bool Contains(this string source, string toCheck, StringComparison comp)
                  {
                      return source.IndexOf(toCheck, comp) >= 0;
                  }
                  
              }
    • If the file contains the identifier name then add the file name to list string.
      •  String add = Path.GetFileName(file);
                            dtrow.Add(add);
                            add = String.Empty; //To clear string for next file.
    • Now since we are sure that the current file contains at least an occurrence of entered identifier name we will read the contents copied to the string line by line.
      •    using (StringReader reader = new StringReader(contents))
                            {
                                string line;
                                while ((line = reader.ReadLine()) != null)
                                
    • Now check if identifier exists in the current line, again ignoring case
      • if (line.IndexOf(textBox1.Text, StringComparison.OrdinalIgnoreCase) >= 0)
    • Now if identifier exists in that line, count the total number of occurrences of the identifier in that line.
      • while ((pos < line.Length) && (pos = line.IndexOf(textBox1.Text,pos,StringComparison.OrdinalIgnoreCase)) != -1)
                                        {
                                            ofile_occur++;
                                            pos += textBox1.Text.Length;
                                        }
                                        ofile_occur = 0;  // for re-use
      •             pfilr_occur=pfilr_occur+ofile_occur;
    • Now after reading all the lines of a particular file. pfilr_occur contains all the hits of that particular file. Just add that to list<string> and that list string as a row to data table. Don't forget to clear the list string to re-use and the file occurrence variable
      • dtrow.Add(pfilr_occur.ToString());
                            dt.Rows.Add(dtrow.ToArray<string>());
                            dtrow.Clear();           
                            pfilr_occur = 0;.
    • Repeat the above steps for each and every file.
    • Finally after traversing through all the files in all the directories as well as sub-directories as specified by the user data table dt is ready.
    • Now just populate the excel sheet using that data table. 
    • Excel sheet is ready make it visible to use and save the excel sheet, if you want to specify a unique path for the excel file for the sake of file versioning
    • Note: Folder Browser dialog is used for user to select a directory.
      • private void button2_Click(object sender, EventArgs e)
                {
                    DialogResult result = folderBrowserDialog1.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        dir = folderBrowserDialog1.SelectedPath;
                        label3.Text = dir;
                        tt.SetToolTip(label3, label3.Text);  // How to set tool tip in c#
                    }
                }
    • Also if the identifier name is not found then a message box should be displayed.
      • else
                    {
                        MessageBox.Show("Column name not found in any of the files in specified directory");
                    }
    File name along with number of hits in c#
    File name along with number of hits in c#

    Source Code



    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Diagnostics;
    using System.Text.RegularExpressions;
    using Microsoft.Office.Interop.Excel;

    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            System.Data.DataSet dataSet = null;
            System.Data.DataTable dt = new System.Data.DataTable("sak_impact");
            int file_count = 0;
            int occur_count=0;
            int total = 0;
            int ofile_occur = 0;
            int pfilr_occur = 0;
            
            List<String> dtrow=new List<String>();

            ToolTip tt = new ToolTip();
            String dir = String.Empty;
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                int emflag = 0;
                if (String.IsNullOrEmpty(textBox1.Text))
                {
                    MessageBox.Show("Please enter identifier name");
                    return;
                }

                if (String.IsNullOrEmpty(dir))
                {
                    MessageBox.Show("Please Choose a directory");
                    return;
                }
                if(dt.Columns.Count==0)
               {
                dt.Columns.Add("Programs referencing SAK");
                dt.Columns.Add("Total No. of Hits for SAK");
                }
                foreach (string file in Directory.EnumerateFiles(dir, "*", SearchOption.AllDirectories))
                {
                    string contents = File.ReadAllText(file);
                    if (contents.IndexOf(textBox1.Text, StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        emflag = 1;
                        String add = Path.GetFileName(file);
                        dtrow.Add(add);
                        add = String.Empty;
                        using (StringReader reader = new StringReader(contents))
                        {
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                  if (line.IndexOf(textBox1.Text, StringComparison.OrdinalIgnoreCase) >= 0)
                                {
                                    
                                    int pos=0;                 
                                    while ((pos < line.Length) && (pos = line.IndexOf(textBox1.Text,pos,StringComparison.OrdinalIgnoreCase)) != -1)
                                    {
                                        ofile_occur++;
                                        pos += textBox1.Text.Length;
                                    }
                                    pfilr_occur=pfilr_occur+ofile_occur;
                                    ofile_occur = 0;
                                   
                                }
                           }
                            
                        }
                        dtrow.Add(pfilr_occur.ToString());
                        dt.Rows.Add(dtrow.ToArray<string>());
                        dtrow.Clear();           
                        pfilr_occur = 0;
                                    }
                }
              
                if (emflag == 1)
                {
                    dataSet = new DataSet("General");
                    dataSet.Tables.Add(dt);
                    Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
                    Workbook xlWorkbook = ExcelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

                    // Loop over DataTables in DataSet.
                    DataTableCollection collection = dataSet.Tables;

                    Sheets xlSheets = null;
                    Worksheet xlWorksheet = null;
                    //Create Excel Sheets
                    xlSheets = ExcelApp.Sheets;
                    xlWorksheet = (Worksheet)xlSheets.Add(xlSheets[1],
                                   Type.Missing, Type.Missing, Type.Missing);

                    System.Data.DataTable table = collection[0];
                    xlWorksheet.Name = table.TableName;
                    for (int j = 1; j < table.Columns.Count + 1; j++)
                    {
                        ExcelApp.Cells[1, j] = table.Columns[j - 1].ColumnName;
                    }
                   
                    // Storing Each row and column value to excel sheet
                    for (int k = 0; k < table.Rows.Count; k++)
                    {
                      for (int l = 0; l < table.Columns.Count; l++)
                        {
                            ExcelApp.Cells[k + 2, l + 1] =
                            table.Rows[k].ItemArray[l].ToString();

                        }
                    }
                    ExcelApp.Application.ActiveWindow.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMaximized;
                    ExcelApp.Application.ActiveWindow.FreezePanes = false;
                    ExcelApp.Application.ActiveWindow.SplitColumn = 1;
                    ExcelApp.Application.ActiveWindow.SplitRow = 1;
                    ExcelApp.Application.ActiveWindow.FreezePanes = true;
                    ExcelApp.Columns.AutoFit();
                  //  xlWorkbook.SaveAs("");
                    ExcelApp.Visible = true;
                    ((Worksheet)ExcelApp.ActiveWorkbook.Sheets[ExcelApp.ActiveWorkbook.Sheets.Count]).Delete();
                            
                }
                else
                {
                    MessageBox.Show("Column name not found in any of the files in specified directory");
                }
            }

            private void label2_Click(object sender, EventArgs e)
            {

            }

            private void button2_Click(object sender, EventArgs e)
            {
                DialogResult result = folderBrowserDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    dir = folderBrowserDialog1.SelectedPath;
                    label3.Text = dir;
                    tt.SetToolTip(label3, label3.Text);
                }
            }

        }
        public static class StringExtensions
        {
            public static bool Contains(this string source, string toCheck, StringComparison comp)
            {
                return source.IndexOf(toCheck, comp) >= 0;
            }
            
        }
    }

    0 comments:

    Post a comment