Pages

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 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.

Tweets

Twitter Updates

Opinions expressed in this blog are my own personal opinions and do not represent the opinions of my employer in any way. Code and information in this blog should be used at your own risk. I take no responsibility for any problems that may arise by anyone using code or information in this blog. Again, you may only use the code or information on this blog at your own risk.