On May 2, 2006, at 5:16 PM, Nathan Olberding wrote:

> I'm trying to write a simple script to go through all the files in a
> specific directory and search for search terms. You should,
> hypothetically, be able to run:
>
> search search_term
>
> and find all the applicable results. Here's my code:
>
> path = "/home/nathano/notes"
> notes_dir = Dir.new(path).to_a - ['.', '..']
> matches = []
>
> notes_dir.each do |note|
> 	ARGV.each do |pattern|
> 		file_contents = open(path + "/" + note, "r")
> 		file_contents.each do |line|
> 			if line.include?(pattern.to_s)
> 				matches.push(note)
> 			end
> 		end
> 	end
> end
>
> matches.uniq.each do |x|
> 	puts x
> end
>
>
> It seems to actually work on rare occasion, but obviously it isn't
> working as expected. I've also tried converting each file to a massive
> string (readlines.to_s) and "include?"ing that string, but to no  
> avail.
>
> Insights?
>
> -- 
> Posted via http://www.ruby-forum.com/.
>

Have you seen File.find ?
http://www.ruby-doc.org/stdlib/libdoc/find/rdoc/classes/Find.html