Monday, October 22, 2012

LINQPad Source Code Searcher

As I had been working in a Classic ASP environment in the past, there were many database procedures that we still used on a day to day basis and there was need to be able to make changes to our new system, written in C# MVC 3 Razor. If anyone has had to work in classic ASP, you can understand just how disjointed it can be having database calls mixed in with your presentation layer (bleh!).

So my big challenge was that anytime a modification needed to be done and we weren't sure whether or not the procedure that we were about to deploy was being used elsewhere, I decided using LINQPad (I love this tool), that I would write a very simple script that would go out and search all of our Source and find all of the procedures that were being called and list out the filename that was actually involved.

void Main()
    DirectoryInfo d = new DirectoryInfo("c:\\{dir}\\{dir}");
    // Directories that we don't want to scan
    List<string> OmitDirectories = new List<string>();
    var dirList = d.GetFiles("*.asp", SearchOption.AllDirectories)
        .Select(x => x.FullName);

    foreach (var omit in OmitDirectories)
        dirList = dirList.Where(x => !x.Contains(omit)).ToList();
    List<ProcData> Procs = new List<ProcData>();
    int counter = 0;
    int found = 0;
    foreach (string filename in dirList)
        string text = File.ReadAllText(filename);
        Regex regexObj = new Regex(@"\b([-A-Z.]+)(\.dbo\.)([-A-Z.]+)|(dbo\.)([-A-Z.]+)?", RegexOptions.IgnoreCase);
        Match matchResults = regexObj.Match(text);
        string proc = string.Empty;
        if (matchResults.Success)
            for (int i = 1; i < matchResults.Groups.Count; i++) {
                Group groupObj = matchResults.Groups[i];
                proc += groupObj.Value;
            Procs.Add(new ProcData() { Proc = proc, FileName = filename });
            if (found % 100 == 0)
                Console.WriteLine("Found:{0}...", found);
        string message = string.Format("Reading :{0}", filename);
        if (counter % 1000 == 0)
            Console.WriteLine("Records Read:{0}", counter);
    Console.WriteLine("Read through {0} files", counter);

public class ProcData
    public string Proc { get; set; }
    public string FileName { get; set; }

No comments:

Post a Comment