utime permission denied in windows 7 when call FileUtils.touch(f)

826 Views Asked by At

I'm working with ruby 2.0 on windows 7 (unfortunately I have to) and have issue with this code:

FileUtils.touch(file)

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.

How can I deal with it error?

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

example:

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

There are 1 best solutions below

1
On

I tested with ruby 1.9 and 2.0. FileUtils.touch works without problems.

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?

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 has one parameter: a file name or path to a file.

You have to adapt your example:

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