The standard procedure with 'find' utility, when performing recursive
delete or similiar operations is to use '-depth' option, so it first
processes the files inside, and then directories.
You can adapt the same technique.
Instead of working on file list:
asd/
asd/edc/
asd/edc/rfv
you should work on:
asd/edc/rfv
asd/edc
asd/
···
On 2007-06-14 21:17:24 +0900 (Thu, Jun), Rebhan, Gilbert wrote:
OK, so i need to copy the whole structure and to do the substitution of
pathname when creating the new pathname, but i don't get it
--
Ceterum censeo Internet Explorer esse delendam.
In fact you may choose two ways:
1) copy the structure
2) rename just the files inside
If you choose 1), then you should process the top items first (the
opposite I have suggested), and if you choose 2) then you should process
the deepest items first.
I don't recall any already-ready object which will do it for you, but you can write
your own recursive directory parser, walk thru the directory structure and rename
(File.rename) each file which matches your pattern. Just remember to
call the recursive function on found direcotry before you rename it, and
everything should be OK.
Just use Dir['*'] not Dir['**/**']
···
On 2007-06-14 21:53:17 +0900 (Thu, Jun), Rebhan, Gilbert wrote:
Hi,
-----Original Message-----
From: Mariusz Pekala [mailto:skoot@ideico.net]
Sent: Thursday, June 14, 2007 2:35 PM
To: ruby-talk ML
Subject: Re: Dir copy with rename
/*
The standard procedure with 'find' utility, when performing recursive
delete or similiar operations is to use '-depth' option, so it first
processes the files inside, and then directories.
*/
OK i'll have a look
I have =
require 'fileutils'
srcdir="Y:/test"
destdir="Y:/test_"
Dir.mkdir(destdir) unless File.exists?(destdir)
Dir.entries(srcdir).each do | i |
if i !='.' && i !='..' && i !=~ /\./
FileUtils.cp_r Dir["#{srcdir}/**"], destdir,:verbose => true
end
end
my problem =
i don't know how to put the rename part in,
and how to do it without fileutils, because i have to do it
with jruby, and jruby doesn't have fileutils
"No such file to load -- fileutils"
Regards, Gilbert
--
Ceterum censeo Internet Explorer esse delendam.
Rebhan, Gilbert wrote:
my problem =
i don't know how to put the rename part in,
and how to do it without fileutils, because i have to do it
with jruby, and jruby doesn't have fileutils
"No such file to load -- fileutils"
JRuby does have fileutils...are you sure your environment is set up right to load it?
- Charlie
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
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
Hi,
should have been, read as=
a small improvement to _not_ get files renamed also, as
i want only dirs renamed.
sorry
Gilbert
···
-----Original Message-----
From: Rebhan, Gilbert [mailto:Gilbert.Rebhan@huk-coburg.de]
Sent: Friday, June 15, 2007 12:58 PM
To: ruby-talk ML
Subject: Re: [Solved] Dir copy with rename
Hi,
a small improvement to get files renamed also, as
i want only dirs renamed.
[ ... ]
We do build and distribute a jruby-complete jar, just not through the
standard download location. Instead it gets pushed to the maven
repository. I suppose we could put a copy in the download directory
as well...
http://repo1.maven.org/maven2/org/jruby/jruby-complete/
If you have any more questions related to JRuby, please feel free to
bring them to the JRuby lists:
http://xircles.codehaus.org/projects/jruby/lists
/Nick
···
On 6/15/07, Rebhan, Gilbert <Gilbert.Rebhan@huk-coburg.de> wrote:
Hi,
-----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
if someone else runs into that ...
/*
**********
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
**********
*/
to make it work with ant and <script> you have to use
the jruby-complete.jar in %ANT_HOME%/lib instead of the
'normal' jruby.jar.
the jruby-complete.jar is not available as binary download
as far as i know ( for version 0.9.8 which i use)
you have to build it yourself with jrubysrc and build.xml,
target name="jar-complete"
then all libraries you import with require are included.
Nevertheless the same script doesn't work as expected
when running with jruby, maybe fixed in version 1.0R3
don't know; but that's another story ...