Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Simple File.find (See related posts)

# Simple File.find by c00lryguy
# Thanks to justinwr for adding what I forgot to do
# ------------------------------
# Usage: 
#     * = wildcard in filename
#   File.find("E:\\") => All files in E:\
#   File.find("E:\\Ruby", "*.rb") => All .rb files in E:\Ruby
#   File.find("E:\\", "*.rb", false) => All .rb files in E:\, but not in its subdirs
class File
  def self.find(dir, filename="*.*", subdirs=true)
    Dir[ subdirs ? File.join(dir.split(/\\/), "**", filename) : File.join(dir.split(/\\/), filename) ]
  end
end

Comments on this post

justinwr posts on May 03, 2008 at 23:06
class File 
  def self.find(dir, filename="*.*", subdirs=true) 
  Dir[ subdirs ? File.join(dir.split(/\\/), "**", filename) : File.join(dir.split(/\\/), filename)  ] 
  end 
end 

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts