0

I have to run ruby script on windows 7 (I know that it's bad idea). My script creates folders (if they is not exist) and copies files into them. I'm using FileUtils lib to perform this job, like:

FileUtils.mkdir_p(path)
FileUtils.cp_r(file.path, path)
FileUtils.touch(file)

On ubuntu and mac everything is ok, but on windows 7 machine I got next error:

Permission denied - ./program_folder/input/. (Errno::EACCES)

on this codeline:

Dir.entries('./program_folder/input').map { |file_name| File.new("./program_folder/input/#{file_name}") }.compact

Any ideas how can I fix it?

I have tried to run ruby and irb termianl with administrator access and tried to do FileUtils.chmod_R(0777, @path) on all paths but still no changes...

Extazystas
  • 488
  • 2
  • 11

1 Answers1

1

Your command

Dir.entries('./program_folder/input').map { |file_name| 
  File.new("./program_folder/input/#{file_name}")
}.compact

tries to create a File with the same name as the file/folder you read before.

In detail:

  1. The first file found by Dir.entries('.') is the actual directory (.).
  2. "./program_folder/input/#{file_name}" is ./program_folder/input/. (an existing directory).
  3. This directory path should be the path of a new file.
  4. With File.new you can't open a directory as a File.

Remark after comment:

Inside the Dir.entries you call File.new - that creates a file handle. Without a mode, it tries to open an existing File (File, not Directory!). . is a directory which can't be opended as a file.

If you only want the filename, you don't need the File.new, the string "./program_folder/input/#{file_name}" would be enough. A better solution would be the File.join method:

File.join("./program_folder/input", file_name)

or

File.join(".", "program_folder", "input", file_name)

If you need on real filename, you can check for directories:

Dir.entries('./program_folder/input').map { |file_name|
  "./program_folder/input/#{file_name}" unless File.directory?("./program_folder/input/#{file_name}")
}.compact

or better, you remove the directories:

Dir.entries('.').delete_if{|file_name| 
  File.directory?(file_name)
}
knut
  • 27,320
  • 6
  • 84
  • 112
  • I'm not overwriting files, I'm just create array with all file objects from this Dir. "File.new Opens the file named by filename according to the given mode and returns a new File object." – Extazystas Dec 24 '13 at 06:19
  • See my edit. I think, I now understand what you want to achieve. – knut Dec 24 '13 at 15:17
  • I understand, I've removed '.' and '..' from list with simple checking: Dir.entries('./program_folder/input').map { |file_name| File.new("./program_folder/input/#{file_name}") unless file_name =~ /^\.\.?$/ }.compact And now I have another error with Fileutils in 'utime' permission denied on this code: FileUtils.touch(file) So, thank you! – Extazystas Dec 24 '13 at 23:40
  • You can find new issue by this link: http://stackoverflow.com/questions/20767883/fileutils-touch-in-windows-7 – Extazystas Dec 24 '13 at 23:50