Skip to content

Instantly share code, notes, and snippets.

@micalexander
Created July 20, 2015 18:29
Show Gist options
  • Select an option

  • Save micalexander/86f3e4f89d3bd5b605de to your computer and use it in GitHub Desktop.

Select an option

Save micalexander/86f3e4f89d3bd5b605de to your computer and use it in GitHub Desktop.
Ruby: Get last modified file from a directory
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