检查文件是否比Ruby中的其他文件更新?

时间:2014-09-06 23:35:00

标签: ruby file-io

在Ruby中,如何在复制之前检查文件是否比目标更新?

我在Ruby-doc.org找到了FileUtils.uptodate?,但它似乎无法正常工作。
运行时出现此错误消息:

  

/Users/bprov/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/fileutils.rb:148:in uptodate?': undefined method每个&#39; for&#34; / Users / bprov / folder2 / homecontroller&#34;:String(NoMethodError)       来自Test_Copy.rb:11:block in <main>' from Test_Copy.rb:6:in foreach&#39;       来自Test_Copy.rb:6:在`&#39;

我有类似的东西:

require 'fileutils'

$sourcepath = "/Users/bprov/railsbridge"
$destinationpath = "/Users/bprov/folder2"

Dir.foreach($sourcepath)  do |file|
  if (file =~ /^\./ )
    # Do nothing
  else
    puts file
    if FileUtils.uptodate?($sourcepath + "/" + file, $destinationpath + "/" + file)
      # Do nothing
    else
      # If newer
      FileUtils.cp($sourcepath + "/" + file, $destinationpath + "/" + file)
    end
  end
end

1 个答案:

答案 0 :(得分:4)

所以,只需正确使用它:

FileUtils.uptodate?($sourcepath + "/" + file, [$destinationpath + "/" + file])