Created
July 20, 2015 18:29
-
-
Save micalexander/86f3e4f89d3bd5b605de to your computer and use it in GitHub Desktop.
Ruby: Get last modified file from a directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_last_modified(dir) | |
| # make sure the files to be checked are in the provided directory | |
| files = Dir.new(dir).select { |file| file!= '.' && file!='..' } | |
| # make sure the file has been written to | |
| return nil if (files.size < 1) | |
| # create an array of all the files | |
| files = files.collect { |file| File.join(dir , file) } | |
| # sort array by last modified | |
| files = files.sort { |a,b| File.mtime(b)<=>File.mtime(a) } | |
| # return the last modified file | |
| return files.first | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment