How to Read a File's Modification Date with Ruby

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

Use mtime:

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

How to order files by last modified time in ruby?

How about simply:

# If you want 'modified time', oldest first
files_sorted_by_time = Dir['*'].sort_by{ |f| File.mtime(f) }

# If you want 'directory change time' (creation time for Windows)
files_sorted_by_time = Dir['*'].sort_by{ |f| File.ctime(f) }

Find date file was last modified

You can pass it a string of the file name.

irb(main):001:0> File.mtime("Gemfile")
=> 2016-08-22 13:54:43 -0700

To reference files from within rails you can use Rails.root.join:

gemfile = Rails.root.join("Gemfile")
=> #<Pathname:/Users/username/projects/appname/Gemfile>

File.mtime(gemfile)
=> 2016-08-22 13:54:43 -0700

The docs also mention you can pass it an IO object.

Can I get the date when an HTTP file was modified?

You can use the built-in Net::HTTP library to do most of this for you:

require 'net/http'

Net::HTTP.start('stackoverflow.com') do |http|
response = http.request_head('/robots.txt')

response['Last-Modified']
# => Sat, 04 Jun 2011 08:51:44 GMT
end

If you want, you can convert that to a proper date using Time.parse.

ruby Copy files based on date modified

This is what I ended up doing to get the files modified in the last 10 minutes and then copy them from the a windows share folder to the linux folder:

require 'fileutils'
require 'time'

class Copier
def initialize(from,to)
puts "copying files... puts #{Time.now} \n"

my_files = Dir["#{from}/*.*"].select { |fname| File.mtime(fname) > (Time.now - (60*10)) })
my_files.each do |filename|
name = File.basename(filename)
orig = "#{filename}"
dest = "#{to}/#{name}"
FileUtils.cp(orig, dest)
puts "cp file : from #{orig} => to #{dest}"
end
end
end

Copier.new("/mnt/windows_share", "linux_folder")

In ruby, how might I find all Files between two dates

Perhaps something like this? (untested):

selected_files = Dir.glob("*.pdf").select do |file|
mtime = File.mtime(file)

# if in a rails environment:
# (1.day.ago .. Time.now).cover?(mtime)

# if not in rails environment but want to use that code do this before that line:
# require 'active_support/all'

# else do the math:
# mtime > (Time.now - 86400) and mtime < Time.now
end

What is the best way in ruby to check if a file was actually modified?

require 'digest/sha1'

Digest::SHA1.hexdigest(File.read("/a")) # => "da39a3ee5e6b4b0d3255bfef95601890afd80709"

How to get last modified file in a directory to pass to system commands using Ruby?

There's no need to shell out to ls and parse its output at all. Ruby gives you standard library methods to fetch directory contents and examine file mtimes. Here's a ruby method to return the name of a file in a directory with the latest mtime.

def last_modified_in dir
Dir.glob( File.join( dir,'*' ) ).
select {|f| File.file? f }.
sort_by {|f| File.mtime f }.
last
end

irb> system 'mkdir -p /tmp/foo'
irb> system 'rm /tmp/foo/*'
irb> ('a'..'c').each { |f| system "touch /tmp/foo/#{f}"; sleep 1; }
irb> puts last_modified_in '/tmp/foo'
# => /tmp/foo/c

How do I get the file creation time in Ruby on Windows?

Apparently, the "ctime" ("creation" or "change" time) metadata attribute of a file is system dependent as some systems (e.g. Windows) store the time that a file was created (its "birth date") and others (Posix systems, e.g. Linux) track the time that it was last updated. Windows uses the ctime attribute as the actual creation time, so you can use the various ctime functions in Ruby.

The File class has static and instance methods named ctime which return the last modified time and File::Stat has an instance method (which differs by not tracking changes as they occur).

File.ctime("foo.txt") # => Sun Oct 24 10:16:47 -0700 2010 (Time)

f = File.new("foo.txt")
f.ctime # => Will change if the file is replaced (deleted then created).

fs = File::Stat.new("foo.txt")
fs.ctime # => Will never change, regardless of any action on the file.


Related Topics



Leave a reply



Submit