Get oldest File from Directory?

Hey!

What is the easiest way to retrieve the oldest (oldest modification
date) file from a directory?

thx ck

···

--
Posted via http://www.ruby-forum.com/.

def oldest_file(dir)
  Dir.entries(dir).map { |e|
    File.join(dir, e)
  }.select { |f|
    File.file? f
  }.sort_by { |f|
    File.mtime f
  }.first
end

This ignores directories.

···

2008/4/10, Christian Kerth <christian.kerth@dynamicmedia.at>:

Hey!

What is the easiest way to retrieve the oldest (oldest modification
date) file from a directory?

Stefan Lang wrote:

def oldest_file(dir)
  Dir.entries(dir).map { |e|
    File.join(dir, e)
  }.select { |f|
    File.file? f
  }.sort_by { |f|
    File.mtime f
  }.first
end

Perfect, Thx!

···

--
Posted via http://www.ruby-forum.com/\.