I had to find some files for processing and wanted to find the latest file by modification date. So here is what I came up with:

    Dir.glob("c:/path/to/files/*.*").sort_by {|f| File.mtime(f) }.reverse[0..7]

Explanation

grab the entries in the specified directory

    Dir.glob("c:/path/to/files/*.*")

##pass the entries into the block for sorting

    .sort_by

sort by modification time

    File.mtime(f)

reverse the list and show 0 to 7 entries (which comes out to 8 entries)

    .reverse[0..7]
comments powered by Disqus