From: Jeremy Bopp Date: 2011-02-04T07:50:53+09:00 Subject: Re: Search directory for containing specified string On 2/3/2011 3:37 PM, Bob Hatch wrote: > I am trying to search a directory for all files that contain a specific > string. My code can be found below. Currently, there are no errors, but > it is not giving me filenames for files containing the text string I am > looking for. Hi, Bob. So far you appear to be reimplementing the grep tool in Ruby. It's able to filter files based on regular expressions as well as report files whose contents match regular expressions. There's nothing wrong with doing that, but you do know that you can get grep for Windows, right? It seems to be the right tool for the job here. > search_text = %r{ > .refreshrates().| > .orderclose. > }x > files = Dir.glob("*.MQ4") {|filename| > File.open(filename) do |outfile| > outfile.each_line do |line| > if line =~ search_text then > puts filename > end > end > end > } Have you confirmed that your script is actually processing any files at all? Maybe your glob isn't matching anything. Try adding a puts filename as the first statement of the block given to File.open. If you don't see anything there, check your glob. I also notice that your regexp includes parenthesis and dots. Those have special meaning in regexps, so you need to escape each of them with a single backslash (\) if you want them to match literally. Another thing I notice is that you are assigning the result of running Dir.glob to the files variable. Given what you're doing in this script, you don't need that at all. Just call Dir.glob without assigning its result to anything. -Jeremy