Re: Dir copy with rename

Hi,

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

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/

Hi,

On 2007-06-14 21:53:17 +0900 (Thu, Jun), Rebhan, Gilbert wrote:

The standard procedure with ‘find’ utility, when performing recursive
srcdir=“Y:/test”

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

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[’/’]

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,

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 task though
:frowning:

%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

Hi,

Hi,

should have been, read as=
a small improvement to not get files renamed also, as
i want only dirs renamed.

sorry

Gilbert

On 6/15/07, Rebhan, Gilbert [email protected] wrote:

*/
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 …

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