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:in `block 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:in `copy_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:in `block 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:in
`fu_each_src_dest'
from C:/Ruby192/lib/ruby/1.9.1/fileutils.rb:432:in `cp_r'
from createFolder.rb:28:in `block in <main>'
from createFolder.rb:26:in `each'
from createFolder.rb:26:in `<main>'
Please advice.
Thanks!
···
--
Posted via http://www.ruby-forum.com/\.