0

我在 Windows 7 上使用 ruby​​ 2.0(不幸的是我必须这样做)并且对这段代码有疑问:

FileUtils.touch(file)

需要此代码来更新 file.ctime (这可能也会有问题)所以,当文件被处理时,我“触摸”它们而不是在下一次迭代中处理它们。

我该如何处理它的错误?

ruby_path/fileutils.rb:1137:in 'utime': Permission denied 'path_to_file' Errno::EACCES
'block in touch'
'each'
'touch'

例子:

file = File.new('file_path')
FileUtils.mkdir_p(path)
FileUtils.cp(file.path, path)
FileUtils.touch(file)
4

1 回答 1

0

我用 ruby​​ 1.9 和 2.0 进行了测试。FileUtils.touch工作没有问题。

你能提供一个MWE吗?您是否检查了要检查的文件的权限。

特别是:你确定,你不碰目录吗?

如果您不想检查目录,可以通过以下方式扩展 FileUtils FileUtils.save_touch

require 'fileutils'    
module FileUtils
  def self.save_touch(fpath)
    FileUtils.touch(fpath) unless File.directory?(fpath)
  end
end

FileUtils.save_touch(Dir.pwd)

问题更新后:

FileUtils.touch有一个参数:文件名或文件路径。

你必须调整你的例子:

file = File.new('file_path')
FileUtils.mkdir_p(path)
FileUtils.cp(file.path, path)
FileUtils.touch(file.path)
于 2013-12-26T20:28:27.103 回答