Eko Budi Setiyo
2/16/2005 12:39:00 AM
qewrty wrote:
>Hi,
>
>I've been trying to use FileUtils to recursively copy files from 1 dir
>to another, but it fails due to permission problems.
>
>FileUtils.cp_r( "C:/source_dir/.", "C:/dest_path", :preserve => true )
>
>If possible I'd like to not delete the contents of the destination
>folder and retain permissions of any files I'm not overwriting. Seems
>strange to me that FileUtils.cp_r doesn't take the :force option.
>
>Thanks
>
>
>
>
>
>
Have you try this:
FileUtils.cp_r( "C:/source_dir", "C:/dest_path", :preserve => true )
If you want you can use the following code and save it as "dtools.rb'
#############################Start dtools.rb#############################
require 'ftools'
class String
def str_replace(what,with)
re = Regexp.new(Regexp.quote(what))
self.gsub(re,with)
end
def str_ireplace(what,with)
re = Regexp.new(Regexp.quote(what), Regexp::IGNORECASE)
self.gsub(re,with)
end
end
#----------------------------------------------------------------
module DTools
def DTools.empty?(path=nil)
if Dir.entries(path).length >= 3
false
else
true
end
end
def DTools._delete_all(path=nil)
if File.basename(path) != '.' and File.basename(path) != '..' and path.to_s != @path.to_s
if FileTest.directory?(path) != true
File.safe_unlink(path)
else
if self.empty?(path) == true
Dir.delete(path)
else
Dir.foreach(path){|file|
self._delete_all(path+'/'+file)
}
end
end
end
end
def DTools.delete(path=nil)
@path = File.expand_path(path)
self._delete_all(@path)
Dir.delete(@path)
end
def DTools.copy(from_path=nil,to_path=nil)
if FileTest.directory?(to_path) == false
File.makedirs(to_path)
end
Find.find(from_path) do |f|
to = f.str_replace(from_path,'')
unless to == ''
to = to_path+to
if FileTest.directory?(f) == true
if FileTest.directory?(to) == false
File.makedirs(to)
end
else
File.install(f,to)
end
end
end
end
end
#-----------------------------------------------------------
class << Dir
def delete_all(path=nil)
DTools.delete(path)
end
def empty?(path=nil)
DTools.empty?(path)
end
def copy(from=nil,to=nil)
DTools.copy(from,to)
end
end
##################################End dtools.rb##########################
Than you can do some thing like this
require 'dtools'
Dir.copy("C:/source_dir", "C:/dest_path")
Dir.delete_all('C:/temp')
If Dir.empty?('C:/abcd')
puts 'Directory C:/abcd is empty'
end
I always use 'dtools' on my code, never have any problem.