Wednesday, February 16, 2011

Recursive File Content Search Script in Ruby

Update (2011-02-26): Please note that this program has limitations. For example, currently it will only find one occurence of the search term per line. So if the search term were on a line more than one time you would get incorrect counts. I hope to deal with things like this in a later post.

I've had a couple of situations in the past month where I've wanted to search for a string in every file (essentially) in the application code that I'm working on. In the case today I wanted to find the string "new OleDbCommand(" and count the number of instances.

The code I came up with is as follows:
require 'fileutils'

search_term = "new OleDbCommand("
search_term_count = 0

Dir.glob("**/*.*") do | file_path |
    puts file_path
    
    # Ignore Empty Files
    if File.zero?(file_path) == false    
        File.open(file_path, 'r') do | file |
            file.each_line do | line |
                if line.index(search_term) != nil
                    puts line
                    search_term_count += 1
                end
            end
        end
    end
end

puts
puts "Search Term: '#{search_term}' found #{search_term_count} times."
The most interesting part that I want to point out is the code that gets all of the filenames for me which is line 6. It reads:
Dir.glob("**/*.*") do | file_path |
:
The Dir.glob() function has takes a pattern which is used to tell the function what filename to find. the ** at the start of the pattern tells glob() to do a recursive search through all child directories all the way down as far as it can go. I found this out via a recent post by Rob Connery here. Note at the moment the whole pattern as I have it is not really necessary. The /*.* part just says find all files, but you can change it by changing the * after the dot to specify a type of file (i.e. the file extension) that you want to find. I've put it in there because 1. I started looking for only *.cs files, but then wanted to search globally.

In another post I'm hoping to expand this example to allow this script to do thinks like allowing the search term, search directory, file type and alter some other aspects of the code on the command line when running the script.

No comments:

Post a Comment