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’
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:in cp_r’
from createFolder.rb:28:in block in <main>' from createFolder.rb:26:in each’
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.
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:in cp_r’
from createFolder.rb:28:in block in <main>' from createFolder.rb:26:in each’
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.