From: Robert Klemme Date: 2011-02-04T19:07:29+09:00 Subject: Re: Search directory for containing specified string On Thu, Feb 3, 2011 at 10: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. > > search_text = %r{ >  .refreshrates().| Do you want to look for literal parentheses? Otherwise this empty capturing group is obsolete. Not that it should prevent matches though. >  .orderclose. > }x > files = Dir.glob("*.MQ4") {|filename| Are file extensions really uppercase? If unsure you could as well do Dir.glob "*.{MQ,mq}4" >      File.open(filename) do |outfile| >        outfile.each_line do |line| >    if line =~ search_text then >        puts filename >          end >        end >      end > } Hm, even if they are not text files you may be able to find a match. I would not bet on it though - mixing binary data with regular expressions is not exactly intended use. :-) You could compare results with egrep -l 'refreshrates|orderclose' *.MQ4 Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/