Rebhan, Gilbert
6/15/2007 10:58:00 AM
Hi,
a small improvement to get files renamed also, as
i want only dirs renamed.
[ ... ]
def rename(headOfTree, what, withWhat)
p = Pathname.new(headOfTree)
p.find() do |file|
path = file.to_s #just to make it prettier
File.rename(path, path.gsub("#{what}", withWhat)) if
path.include?(what) && File.directory?(file)
end
end
Find.find(destdir) do |file|
rename(file,torepl,replwith)
end
&& File.directory?(file)
was missing rename method
Regards, Gilbert
-----Original Message-----
From: Rebhan, Gilbert [mailto:Gilbert.Rebhan@huk-coburg.de]
Sent: Friday, June 15, 2007 12:44 PM
To: ruby-talk ML
Subject: [Solved] Dir copy with rename
Hi,
finally with your help and the snippets of another thread =
'Directory tree traversal fun'
i have a working solution with Ruby 1.8.4 (all-in-one) on windows.
is there anything wrong, something that could be done better
in following snippet ?
require 'fileutils'
require 'find'
require 'pathname'
srcdir="Y:/test"
destdir="Y:/test_"
torepl="foobar"
replwith="foobaz_"
# Method from thread 'Directory tree traversal fun'
def rename(headOfTree, what, withWhat)
p = Pathname.new(headOfTree)
p.find() do |file|
path = file.to_s #just to make it prettier
File.rename(path, path.gsub("#{what}", withWhat)) if
path.include?(what)
end
end
Dir.mkdir(destdir) unless File.exists?(destdir)
Dir.entries(srcdir).each do | i |
if i !='.' && i !='..'
FileUtils.cp_r Dir["#{srcdir}/**"], destdir
end
end
Find.find(destdir) do |file|
if File.directory?(file)
rename(file,torepl,replwith)
end
end
**********
still it's not working with jruby within ant via <script> task though
:-(
%JRUBY_HOME% set
%JRUBY_HOME%/bin in path
jruby.jar in %ANT_HOME%/lib
**********
thanks in advance !!
Regards, Gilbert