Cp_r is not working

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 = Time.now.strftime(’%Y-%m-%d’)
srcPath = “C:/Users/eliranbz/Desktop/Test”
destPath = time + ‘/’
#Create the Files Array
theFiles = []
#Check if folder exist
if File.exists?(destPath) && File.directory?(destPath)
puts ‘Folder could not be created - Already exist’
puts 'Folder has been created: ’ + destPath
else
FileUtils.mkdir time
puts 'Folder has been created: ’ + time
end
#Just to know how much files I have in the Directory
filesCount = Dir.entries(srcPath).size - 2
puts 'files count: ’ + filesCount.to_s

#Insert File by File to the Array

Dir.foreach(srcPath) {
|x| theFiles << x
puts x
}

puts ‘Files has been inserted to Array’

#Make a copy on each file value in the array

theFiles.each do |y|
FileUtils.cp_r y, destPath + y
end

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) from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:461:inblock in
copy_entry’

    from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:1336:in

preorder_traverse' from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:458:incopy_entry’
from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:433:in block in cp_r' from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:1418:inblock in
fu_each_sr
c_dest’
from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:1432:in
fu_each_src_dest0' from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:1416:infu_each_src_dest’
from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:432:in cp_r' from createFolder.rb:28:inblock in ’
from createFolder.rb:26:in each' from createFolder.rb:26:in

Please advice.

Thanks!

On Wed, Feb 2, 2011 at 1:46 PM, Eliran Bz [email protected] wrote:

Here is my code:
puts ‘Folder could not be created - Already exist’

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|
FileUtils.cp_r y, destPath + y
end

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

bin
README.rdoc
from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:1336:in
fu_each_src_dest' from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:432:incp_r’
from createFolder.rb:28:in block in <main>' from createFolder.rb:26:ineach’
from createFolder.rb:26:in `’

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

Thanks all, I figure it out, It tried to copy the folder itself to
itself which cause a conflict.

Robert,

How can I SKIP the “.” and “…”?
Do you have an Idea?

Thank you all for your answers!

Robert K. wrote in post #979115:

On Wed, Feb 2, 2011 at 1:46 PM, Eliran Bz [email protected] wrote:

Here is my code:
puts ‘Folder could not be created - Already exist’

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|
FileUtils.cp_r y, destPath + y
end

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

bin
README.rdoc
from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:1336:in
fu_each_src_dest' from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:432:incp_r’
from createFolder.rb:28:in block in <main>' from createFolder.rb:26:ineach’
from createFolder.rb:26:in `’

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

Eliran Bz a écrit :

How can I SKIP the “.” and “…”?
Do you have an Idea?

One way:

Dir.foreach(path) do |file|
next if [’.’, ‘…’].include?(file)
puts file
end

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