Stan K.
Stan K.

Reputation: 487

Have to run ruby script on Windows 7 and got Permission denied EACCES

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...

Upvotes: 0

Views: 928

Answers (1)

knut
knut

Reputation: 27875

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)
}

Upvotes: 1

Related Questions