调用FileUtils.touch(f)时Windows 7中的utime权限被拒绝 [英] utime permission denied in windows 7 when call FileUtils.touch(f)
问题描述
我正在Windows 7上使用ruby 2.0(不幸的是,我必须这样做),并且此代码有问题:
I'm working with ruby 2.0 on windows 7 (unfortunately I have to) and have issue with this code:
FileUtils.touch(file)
此代码是更新file.ctime所必需的(可能也会出现问题) 因此,在处理文件时,我会触摸"它们,而不在下一次迭代中对其进行处理.
This code is needed to update file.ctime (which probably will have problems too) So, when files is processed I 'touch' them and not processing them in next iteration.
如何处理错误?
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)
推荐答案
我使用ruby 1.9和2.0进行了测试. FileUtils.touch
可以正常工作.
I tested with ruby 1.9 and 2.0. FileUtils.touch
works without problems.
您可以提供MWE吗?您是否检查了要检查文件的权限.
Can you provide a MWE. Did you check the permissions on the file you want to check.
特别是:您确定不触摸目录吗?
Especially: Are you sure, you don't touch a directory?
如果您不想检查目录,则可以通过FileUtils.save_touch
扩展FileUtils:
If you don't want to check for directories, you may extend FileUtils by 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)
问题更新后:
After update of question:
FileUtils.touch
具有一个参数:文件名或文件路径.
FileUtils.touch
has one parameter: a file name or path to a file.
您必须调整示例:
file = File.new('file_path')
FileUtils.mkdir_p(path)
FileUtils.cp(file.path, path)
FileUtils.touch(file.path)
这篇关于调用FileUtils.touch(f)时Windows 7中的utime权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!