ryan1393402
ryan1393402

Reputation: 715

Is it possible to read a file's modification date with Ruby?

Is it possible to read a file's modification date with Ruby? I have successfully opened a text file and captured the contents of the file with

File.open("test.txt", "r").each do |line|"

but it would be very useful to read the modification date of the file.

Upvotes: 62

Views: 27759

Answers (3)

Artur INTECH
Artur INTECH

Reputation: 7286

If you have already open the file (like you did in your example) it might be a better idea to use the instance method File#mtime instead:

File.new('file_path').mtime

Upvotes: 0

Constantine M
Constantine M

Reputation: 1547

Use mtime:

File.mtime("testfile")
=> 2014-04-13 16:00:23 -0300

Upvotes: 107

Matzi
Matzi

Reputation: 13925

"Returns the modification time for the named file as a Time object."

File.mtime("testfile")   #=> Tue Apr 08 12:58:04 CDT 2003

Check this.

Upvotes: 27

Related Questions