[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

copy a folder and its contents

aidy

8/21/2008 2:58:00 PM

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
5 Answers

Lex Williams

8/21/2008 2:59:00 PM

0

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

aidy

8/21/2008 3:12:00 PM

0

Hi Lex,

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

> use cp_r
> --
> Posted viahttp://www.ruby-....

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

Robert Klemme

8/21/2008 3:19:00 PM

0

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


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

Michael Morin

8/21/2008 4:35:00 PM

0

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

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

David Masover

8/22/2008 4:04:00 AM

0

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.

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'