Copy a folder and its contents

Hi,

I am looking at copying a folder and its contents from one destination
to another.

FileUtils.copy(“C:/rspec_reports/javascripts”, “C:/rspec_reports/
javascripts/test”)

However, it seems FileUtils.copy is just for files

Is there a copy directory method in the Ruby libary?

Thanks

Aidy

aidy wrote:

Hi,

I am looking at copying a folder and its contents from one destination
to another.

FileUtils.copy(“C:/rspec_reports/javascripts”, “C:/rspec_reports/
javascripts/test”)

However, it seems FileUtils.copy is just for files

Is there a copy directory method in the Ruby libary?

Thanks

Aidy

use cp_r

Hi Lex,

On Aug 21, 3:59 pm, Lex W. removed[email protected] wrote:

use cp_r

Posted viahttp://www.ruby-forum.com/.

Thanks, I had previously tried it and received this recursive
exception

No such file or directory - C:/rspec_reports/javascripts/test/
javascripts/test/javascripts/test/javascripts/test/javascripts/test/
javascripts/test/javascripts/test/javascripts/test/javascripts/test/
javascripts/test/javascripts/test/javascripts/test/javascripts/test/
javascripts

Aidy

2008/8/21 aidy [email protected]:

No such file or directory - C:/rspec_reports/javascripts/test/
javascripts/test/javascripts/test/javascripts/test/javascripts/test/
javascripts/test/javascripts/test/javascripts/test/javascripts/test/
javascripts/test/javascripts/test/javascripts/test/javascripts/test/
javascripts

This comes as no surprise because you are trying to copy a folder
hierarchy into a folder which is inside that hierarchy. In other
words: the recursion is in your copying. Are you sure this is what you
want?

Kind regards

robert

On Thursday 21 August 2008 11:35:04 Michael M. wrote:

files = Dir.glob(’*’)
FileUtils.mkdir ‘backup’
FileUtils.cp_r files, ‘backup’

This avoids cp_r trying to descend into the folder it’s copying into.

Well, only if “backup” doesn’t already exist. Maybe:

files = Dir.glob(’*’) - [‘backup’]
FileUtils.mkdir ‘backup’ unless Dir.exists? ‘backup’
FileUtils.cp_r files, ‘backup’

Robert K. wrote:

Kind regards

robert

It looks like he’s just trying to make a backup of all the files in a
directory. Try something like this.

files = Dir.glob(’*’)
FileUtils.mkdir ‘backup’
FileUtils.cp_r files, ‘backup’

This avoids cp_r trying to descend into the folder it’s copying into.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs