On Wed, Feb 2, 2011 at 1:46 PM, Eliran Bz <eliranbz / gmail.com> wrote:
> Hi,
>
> I'm trying to build a program which copy from source folder to dest
> folder.
> I did it in File-After-File methond which mean I inserted all the file
> names into an Array and then make a cp_r on each file.
>
> It's not working and I get errors of Folder Duplication.
> I can't understand why, I'm kinda new to Ruby.
>
> Here is my code:
>
> require 'FileUtils'
>
> time =3D Time.now.strftime('%Y-%m-%d')
> srcPath =3D "C:/Users/eliranbz/Desktop/Test"
> destPath =3D time + '/'
> #Create the Files Array
> theFiles =3D []
> #Check if folder exist
> if File.exists?(destPath) && File.directory?(destPath)
> =A0 =A0puts 'Folder could not be created - Already exist'
> =A0puts 'Folder has been created: ' + destPath
> else
> =A0FileUtils.mkdir time
> =A0puts 'Folder has been created: ' + time
> end
> #Just to know how much files I have in the Directory
> filesCount =3D Dir.entries(srcPath).size - 2
> puts 'files count: ' + filesCount.to_s
>
> #Insert File by File to the Array
>
> Dir.foreach(srcPath) {
> |x| theFiles << x

You need do prepend the path, e.g.

theFiles << File.join(srcPath, x)

Also, you need to know that "." and ".." are included in the list of
entries found.

> puts x
> }
>
> puts 'Files has been inserted to Array'
>
> #Make a copy on each file value in the array
>
> theFiles.each do |y|
> =A0FileUtils.cp_r y, destPath + y
> end

cp_r ("r" means recursive) only really makes sense for directories.

> I get the error:
>
> C:\Users\eliranbz\Desktop>ruby createFolder.rb
> Folder could not be created - Already exist
> Folder has been created: 2011-02-02/
> files count: 17
> .
> ..
> .autotest
> .document
> bin
> ChangeLog
> cruise_config.rb
> GPL.txt
> hide_lib_for_update
> History.txt
> lib
> LICENSE.txt
> Manifest.txt
> pkgs
> Rakefile
> README.rdoc
> setup.rb
> test
> util
> Files has been inserted to Array
> C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:1244:in `copy': cannot copy
> directory 201
> 1-02-02 to itself 2011-02-02/././2011-02-02 (ArgumentError)
> =A0 =A0 =A0 =A0from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:461:in `block =
in
> copy_entry'
>
> =A0 =A0 =A0 =A0from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:1336:in
> `preorder_traverse'
> =A0 =A0 =A0 =A0from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:458:in `copy_e=
ntry'
> =A0 =A0 =A0 =A0from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:433:in `block =
in
> cp_r'
> =A0 =A0 =A0 =A0from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:1418:in `block=
 in
> fu_each_sr
> c_dest'
> =A0 =A0 =A0 =A0from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:1432:in
> `fu_each_src_dest0'
> =A0 =A0 =A0 =A0from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:1416:in
> `fu_each_src_dest'
> =A0 =A0 =A0 =A0from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:432:in `cp_r'
> =A0 =A0 =A0 =A0from createFolder.rb:28:in `block in <main>'
> =A0 =A0 =A0 =A0from createFolder.rb:26:in `each'
> =A0 =A0 =A0 =A0from createFolder.rb:26:in `<main>'
>
> Please advice.
>
> Thanks!

Why don't you just do

FileUtils.cp_r srcPath, time

?

Btw, conventionally in Ruby we use CamelCase only for class names
while for all other names we use names_with_underscores.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/