DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Get The Last Modified File In Any Given Directory

08.03.2005
| 2766 views |
  • submit to reddit
        Gives you the last modified file in any given directory.
Returns nil if no files are found.
	def get_last_modified(dir)
		files = Dir.new(dir).select { |file| file!= '.' && file!='..' }
		return nil if (files.size < 1)
		files = files.collect { |file| dir+'/'+file }
		files = files.sort { |a,b| File.mtime(b)<=>File.mtime(a) }
		return File.open(files.first, "r")
	end