Вопрос

I need to get the modification time of a group of files on a server. I know how to get this on a local computer, but File.mtime doesn't work via FTP. How would I convert this code to work on a server?

files_sorted_by_time = Dir['*'].select { |f| 
  ((Time.now - File.mtime(f)).to_i / 604800) < 7 
}
Это было полезно?

Решение

You want Net::FTP#mtime.

Example from documentation:

Net::FTP.open('ftp.netlab.co.jp') do |ftp|
  ftp.login
  files = ftp.chdir('pub/lang/ruby/contrib')
  files = ftp.list('n*')
  ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)
  ftp.mtime('file.pdf')
end

You can use #mtime with #nlst to filter through the list of remote files.

Net::FTP.open('ftp.netlab.co.jp') do |ftp|
  ftp.login
  ftp.nlst do |file|
    if ftp.mtime(file) # ...
  end
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top