ruby Ruby:从目录中获取最后修改的文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Ruby:从目录中获取最后修改的文件相关的知识,希望对你有一定的参考价值。

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

以上是关于ruby Ruby:从目录中获取最后修改的文件的主要内容,如果未能解决你的问题,请参考以下文章