Dir copy with rename

Hi,

···

-----Original Message-----
From: Robert Klemme [mailto:shortcutter@googlemail.com]
Sent: Thursday, June 14, 2007 12:05 PM
To: ruby-talk ML
Subject: Re: Dir copy with rename

Dir[src+'/**/**/**'].each { |old|
  next if old == '.'||old== '..'||old =~ /\./
  puts old
  if re.match(old)
    new = old.sub(re,sub)
    File.rename(old,new)
    end
}

/*
Your basic flaw seems to be that although the subject talks about "copy
with rename" your code actually only renames. That will lead to errors
because you change the original directory tree and thus a file further
down the hierarchy of a renamed folder does not exist any more with the
original file name - hence you cannot rename it.
*/

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

tried things like =

Dir[src+'/**/**'].each { |old|
  next if old == '.'||old== '..'||old =~ /\./
  if re.match(old)
     puts old.sub(src,destdir).sub(re,sub)
     Dir.new(old.sub(src,destdir).sub(re,sub))
     File.open(old.sub(src,destdir).sub(re,sub),"w+")
   end
}

but that doesn't work.
Any hints appreciated.

Regards, Gilbert

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.

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

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,

···

-----Original Message-----
From: Charles.O.Nutter@sun.com [mailto:Charles.O.Nutter@sun.com] On
Behalf Of Charles Oliver Nutter
Sent: Friday, June 15, 2007 3:05 AM
To: ruby-talk ML
Subject: Re: Dir copy with rename

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

hm, JRUBY_HOME set, and %JRUBY_HOME%/bin in path =

set
JRUBY_BASE=C:\jruby-0.9.8

C:\WINNT\system32>jruby -v
ruby 1.8.5 (0) [java-jruby0.9.8]

seems problem is the script runs in ant with <script> task;
%ANT_HOME%/lib/jruby.jar available as i use jruby all the
time in ant;
but it seems there's missing something ?! or do i have to use
the most actual release jruby 1.0R3 ?

Regards. Gilbert

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
: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

···

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

%JRUBY_HOME% set
%JRUBY_HOME%/bin in path
jruby.jar in %ANT_HOME%/lib
**********

thanks in advance !!

Regards, Gilbert

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

%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 ...

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

%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 ...