Find files : Utilities « File Directory « Ruby






Find files


require "find"

def findfiles(dir, name)
  list = []
  Find.find(dir) do |path|
    Find.prune if [".",".."].include? path
    case name
      when String
        list << path if File.basename(path) == name
      when Regexp
        list << path if File.basename(path) =~ name
    else
      raise ArgumentError
    end
  end
  list
end

findfiles "/home/hal", "toc.txt"


findfiles "/home", /^[a-z]+.doc/


 








Related examples in the same category

1.delete a folder recursively
2.Delete with condition
3.Get free disk space under unix with df command
4.Get free disk space under windows with dir command
5.Call system function to delete a folder
6.Showing Line Numbers
7.Delete folder older than a certain time
8.Parse line: value|value
9.Change a | delimited file