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

···

--
Posted via http://www.ruby-forum.com/\.

Hi Lex,

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

···

On Aug 21, 3:59 pm, Lex Williams <eta...@yahoo.com> wrote:

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

···

2008/8/21 aidy <aidy.lewis@googlemail.com>:

Hi Lex,

On Aug 21, 3:59 pm, Lex Williams <eta...@yahoo.com> 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

--
use.inject do |as, often| as.you_can - without end

Robert Klemme wrote:

···

2008/8/21 aidy <aidy.lewis@googlemail.com>:

Hi Lex,

On Aug 21, 3:59 pm, Lex Williams <eta...@yahoo.com> 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

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

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.

--
Michael Morin
Guide to Ruby

Become an About.com Guide: beaguide.about.com
About.com is part of the New York Times Company

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'

···

On Thursday 21 August 2008 11:35:04 Michael Morin 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.