File a file starting with certain string : Find « File Directory « Ruby






File a file starting with certain string


require 'find'
module Find
  def match(*paths)
    matched = []
    find(*paths) { |path| matched << path if yield path }
    return matched
  end
  module_function :match
end

must_start_with = "This Ruby program"
Find.match('./') do |p|
  if File.file? p
    open(p) { |f| f.read(must_start_with.size) == must_start_with }
  else
    false
  end
end
# => ["./rubyprog-0.1/README"]

 








Related examples in the same category

1.Find a path
2.Find.find by block
3.prune under file basename
4.Finding the Files
5.Find the MP3s.
6.Find the README files.
7.Finds files that were probably left behind by emacs sessions.
8.Finds all files that are larger than a certain threshold.
9.Finds all files modified more recently than a certain number of seconds ago.
10.Finds all files that haven't been accessed since they were last modified.