# Dir copy with rename

**URL:** <https://www.ruby-forum.com/t/dir-copy-with-rename/106801>\
**Category:** Ruby\
**Created:** [June 14, 2007, 9:02am UTC](https://www.ruby-forum.com/t/dir-copy-with-rename/106801 "2007-06-14T09:02:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RebhanS\_Gilbert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@RebhanS\_Gilbert](https://www.ruby-forum.com/u/RebhanS_Gilbert)\
**Post date:** [June 14, 2007, 9:02am UTC](https://www.ruby-forum.com/t/dir-copy-with-rename/106801/1 "2007-06-14T09:02:39Z")

</div>

Hi,

i want to copy a dir recursive with all files included,  
but rename all dirnames that match a pattern, i.e.

/srcdir  
/bla/foobar/blabla  
/bla/test/subtest/foobar  
/foobar/test  
/foobar

should be

/targetdir  
/bla/foobaz/blabla  
/bla/test/subtest/foobaz  
/foobaz/test  
/foobaz

i tried =

re=/foobar/  
sub=“foobaz”

Dir[‘Y:/test/ **/** ’].each { |old|  
next if old == ‘.’||old== ‘…’||old =~ /./  
unless /re/.match(old)  
new = old.sub(re,sub)  
File.rename(old,new)  
end  
}

the /foobar folder on first level and on second level get renamed, but  
it dosn’t  
work, when i have /foobar on the third level, means

/foobar/foobar/foobar gets  
/foobaz/foobaz/foobar and Exit Code 1

/test/sub3/foobar/foobar gets  
/test/sub3/foobaz/foobar and Exit Code 1

Error message i.e. =  
No such file or directory - Y:/test/sub3/foobar/foobar or  
Y:/test/sub3/foobaz/foobar

Any ideas what’s wrong ?

Regards, Gilbert

---

<div class="post-metadata">

**Author:** ![RebhanS\_Gilbert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@RebhanS\_Gilbert](https://www.ruby-forum.com/u/RebhanS_Gilbert)\
**Post date:** [June 14, 2007, 11:26am UTC](https://www.ruby-forum.com/t/dir-copy-with-rename/106801/2 "2007-06-14T11:26:57Z")

</div>

On 2007-06-14 16:01:30 +0900 (Thu, Jun), Rebhan, Gilbert wrote:

> /foobar
> 
> Error message i.e. =  
> No such file or directory - Y:/test/sub3/foobar/foobar or  
> Y:/test/sub3/foobaz/foobar
> 
> Any ideas what’s wrong ?

Are you sure the code you posted here is the code you really run?

I guess you want ‘if’ instead of ‘unless’, because you want to rename  
the file if the re matches, not if it does not.

Second - you should use re.match(old) not /re/.match(old) since ‘re’  
variable is already a Regexp.

Having these errors I doubt you would have any file renamed.

---

<div class="post-metadata">

**Author:** ![RebhanS\_Gilbert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@RebhanS\_Gilbert](https://www.ruby-forum.com/u/RebhanS_Gilbert)\
**Post date:** [June 14, 2007, 11:55am UTC](https://www.ruby-forum.com/t/dir-copy-with-rename/106801/3 "2007-06-14T11:55:00Z")

</div>

Hi,

---

<div class="post-metadata">

**Author:** ![RebhanS\_Gilbert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@RebhanS\_Gilbert](https://www.ruby-forum.com/u/RebhanS_Gilbert)\
**Post date:** [June 14, 2007, 12:07pm UTC](https://www.ruby-forum.com/t/dir-copy-with-rename/106801/4 "2007-06-14T12:07:01Z")

</div>

On 14.06.2007 11:53, Rebhan, Gilbert wrote:

> > next if old == ‘.’||old== ‘…’||old =~ /./  
> > re=/foobar/
> 
> Y:/test/sub3  
> Y:/test/sub1  
> Y:/test/foobar  
> Y:/test/sub3/foobar  
> Y:/test/sub3/foobar/foobar  
> dircopy.rb:14:in `rename’:  
> No such file or directory - Y:/test/sub3/foobar/foobar or  
> Y:/test/sub3/foobaz/foobar (Errno::ENOENT)

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. Note that Dir[] first  
generates the full list of files, so any changes you do to the  
filesystem during iteration will not be reflected in the list.

Cheers

robert
