hrsetyono
hrsetyono

Reputation: 4464

Ruby - FileUtils copy_file Permission denied on Windows

I'm making gem that copy files from /template directory (inside the gem) into the current directory of the console.

Here's what it looks like:

require "fileutils"

# Get the console's current directory
destination_dir = Dir.pwd

# Home directory of my gem, looks like C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0
home_dir = File.expand_path( "..", File.dirname(__FILE__) )

# Template directory, looks like C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0/template
template_dir = File.join( home_dir, "template" )

FileUtils.copy_file( template_dir, destination_dir )

And I got this error:

C:/Ruby193/lib/ruby/1.9.1/fileutils.rb:1370:in `initialize': Permission denied -
C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0/template (Errno::
EACCES)

I have checked that the directory does exists by running Dir[template_dir].

Any solution? Thanks

UPDATE to answer comments below

@Babai

I added this line before copy_file, but still doesn't work. Am I doing it right?

FileUtils.chmod(0777, template_dir)

@mudasobwa

Here's the result of the code

# puts "#{template_dir} \n #{destination_dir}"
C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0/template
C:/Users/myname/Documents/Test

Upvotes: 2

Views: 2764

Answers (1)

hrsetyono
hrsetyono

Reputation: 4464

My bad. My template directory contains another folders. So I need to use cp_r instead of copy_file

FileUtils.cp_r( template_dir, destination_dir )

Upvotes: 2

Related Questions