Ruby/File Directory/Find

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

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"]



Find a path

require "find"
Find.find("./") { |path| puts path }



Find.find by block

require "find"
Find.find("./") do |path|
  Find.prune if File.basename(path) == "subdir2"
  puts path
end



Finding the Files

require "find"
module Find
  def match(*paths)
    matched = []
    find(*paths) { |path| matched << path if yield path }
    return matched
  end
  module_function :match
end
# Find the empty files.
Find.match("./") { |p| File.lstat(p).size == 0 }



Finds all files modified more recently than a certain number of seconds ago.

require "find"
module Find
  def match(*paths)
    matched = []
    find(*paths) { |path| matched << path if yield path }
    return matched
  end
  module_function :match
end
 
def modified_recently(seconds, *paths)
  time = Time.now - seconds
  Find.match(*paths) { |p| File.lstat(p).mtime > time }
end



Finds all files that are larger than a certain threshold.

require "find"
module Find
  def match(*paths)
    matched = []
    find(*paths) { |path| matched << path if yield path }
    return matched
  end
  module_function :match
end
def bigger_than(bytes, *paths)
  Find.match(*paths) { |p| File.lstat(p).size > bytes }
end



Finds all files that haven"t been accessed since they were last modified.

require "find"
module Find
  def match(*paths)
    matched = []
    find(*paths) { |path| matched << path if yield path }
    return matched
  end
  module_function :match
end
 
def possibly_abandoned(*paths)
  Find.match(*paths) { |p| f = File.lstat(p); f.mtime == f.atime }
end



Finds files that were probably left behind by emacs sessions.

require "find"
module Find
  def match(*paths)
    matched = []
    find(*paths) { |path| matched << path if yield path }
    return matched
  end
  module_function :match
end
def emacs_droppings(*paths)
  Find.match(*paths) do |p|
    (p[-1] == ?~ and p[0] != ?~) or (p[0] == ?# and p[-1] == ?#)
  end
end



Find the MP3s.

require "find"
module Find
  def match(*paths)
    matched = []
    find(*paths) { |path| matched << path if yield path }
    return matched
  end
  module_function :match
end
 
Find.match("./") { |p| ext = p[-4...p.size]; ext && ext.downcase == ".mp3" }
# => ["./Music/The Snails - Red Rocket.mp3",
#     "./Music/The Snails - Moonfall.mp3",
#     "./Music/cancelled_download.MP3"]



Find the README files.

require "find"
module Find
  def match(*paths)
    matched = []
    find(*paths) { |path| matched << path if yield path }
    return matched
  end
  module_function :match
end
p Find.match("./") { |p| File.split(p)[1] == "README" }



prune under file basename

require "find"
Find.find("./") do |path|
  if File.basename(path) =~ /file2$/
    puts "PRUNED #{path}"
    Find.prune
  end
  puts path
end