# Recursive directory rename problem

**URL:** <https://rubytalk.org/t/recursive-directory-rename-problem/51567>\
**Category:** ruby-talk\
**Created:** [5 February 2009 22:54 UTC](https://rubytalk.org/t/recursive-directory-rename-problem/51567 "2009-02-05T22:54:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregg\_Yows](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@Gregg\_Yows](https://rubytalk.org/u/Gregg_Yows)\
**Post date:** [5 February 2009 22:54 UTC](https://rubytalk.org/t/recursive-directory-rename-problem/51567/1 "2009-02-05T22:54:51Z")

</div>

Problem:  
- trying to standardize on a naming convention for all of my  
directories(OS agnostic).  
- new structure should be all lower-case with hyphphens for spaces  
- Using windows, I am running the following:

Dir.foreach("./"){ |d|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next if d =~ /[a-z]+\.[a-z]+/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next if d == "." or d == ".."  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d.downcase!  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newfile = d.gsub(/\W/, "-")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(d, newfile)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

so I want:

\Test Dir1  
\TestDir2\TestDir 1

to change to

\test-dir1  
\testdir2\testdir-1

Which works fine on the top level, but is not recursing down to the next  
level. Filenames should be ignored.

I've tried playing with various methods on the Dir class (entries,  
etc..).  
Using Dir[\*\*/\*\*] is throwing up saying that it can't rename (no surprise  
since it can't rename without an absolute path, I guess).

Please help thanks!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Mike\_Gold](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Mike\_Gold](https://rubytalk.org/u/Mike_Gold)\
**Post date:** [6 February 2009 05:04 UTC](https://rubytalk.org/t/recursive-directory-rename-problem/51567/2 "2009-02-06T05:04:57Z")

</div>

Ransom Tullis:

> so I want:
> 
> \Test Dir1  
> \TestDir2\TestDir 1
> 
> to change to
> 
> \test-dir1  
> \testdir2\testdir-1
> 
> Which works fine on the top level, but is not recursing down to the  
> next level. Filenames should be ignored.

Dir.entries is not recursive; you want 'find'. But it has to be a  
depth-first 'find': you can gradually replace the pillars of a  
building if you start from the top, but not from the bottom. (Mixing  
metaphors on which direction is 'depth'.)

&nbsp;&nbsp;require 'pathname'

&nbsp;&nbsp;def find\_depth\_first(pathname)  
&nbsp;&nbsp;&nbsp;&nbsp;acc =   
&nbsp;&nbsp;&nbsp;&nbsp;pathname.find { |file| acc \<\< file }  
&nbsp;&nbsp;&nbsp;&nbsp;acc.reverse!  
&nbsp;&nbsp;&nbsp;&nbsp;acc.each { |path| yield path }  
&nbsp;&nbsp;end

&nbsp;&nbsp;find\_depth\_first(Pathname(".")) { |path|  
&nbsp;&nbsp;&nbsp;&nbsp;if path.directory?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_path = path.dirname +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pathname(path.basename.to\_s.downcase.gsub(/\s/, "-"))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if new\_path != path  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path.rename(new\_path)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;}

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
