copy a folder and its contents

A

aidy

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
 
L

Lex Williams

aidy said:
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
 
A

aidy

Hi Lex,


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
 
R

Robert Klemme

2008/8/21 aidy said:
Hi Lex,



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

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
 
M

Michael Morin

Robert said:
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
http://ruby.about.com/
Become an About.com Guide: beaguide.about.com
About.com is part of the New York Times Company
 
D

David Masover

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'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top