Below is a little snippet of code I wrote to recursively find files that matched some substring. It's used like this:
File.recursive_find('/root/search/path', 'invoice')
This will find all of the files that match the string 'invoice' somewhere in their name and return an array of these files. Here's the code:
require 'find'
class File
def self.recursive_find(dir, match_string)
results = []
Find.find(dir) do |path|
unless FileTest.directory?(path)
unless File.basename(path) =~ /#{match_string}/
Find.prune
else
results << path
end
else
next
end
end
results
end
end
No comments:
Post a Comment