Sunday, October 28, 2012

Count Keywords Occurance in a Directory

I searched google for a Word Counter, I wanted to read in files in a directory and output the number of times that word occurs in those files. So I created this command line application that takes an input file of keywords and then searches for those keywords in the local directory and prints out the "File - Keyword - Number of occurances".

I wrote this years ago, today I would have probably written it as a PowerShell script =)



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace CountKeywords
{
    class Program
    {
        public static void Main(string[] args)
        {
            List<string> Keywords = new List<string>();
            StreamReader SR = File.OpenText(args[0]);
            string S = SR.ReadLine();
            while (S != null)
            {
                Keywords.Add(S.Trim());
                S = SR.ReadLine();
            }
            if (!string.IsNullOrEmpty(S))
                Keywords.Add(S.Trim());
            SR.Close();
            string AssemName = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
            List<string> files =Directory.GetFiles(System.IO.Path.GetDirectoryName(AssemName)).ToList();
            foreach (string file in files)
            {
                SR = File.OpenText(file);
                string content = SR.ReadToEnd();
                foreach (string key in Keywords)
                {
                    if (!string.IsNullOrEmpty(content))
                    {
                        Regex reg = new Regex(key, RegexOptions.IgnoreCase);
                        MatchCollection mt = reg.Matches(content);
                        Console.WriteLine("{0},{1},{2}", file, key, mt.Count.ToString());
                    }
                }
                SR.Close();
            }
        }
    }
}

No comments:

Post a Comment