# Rudamentary question

**URL:** <https://www.ruby-forum.com/t/rudamentary-question/162980>\
**Category:** Ruby\
**Created:** [March 16, 2009, 12:25pm UTC](https://www.ruby-forum.com/t/rudamentary-question/162980 "2009-03-16T12:25:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hayden\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cc9497/32.png) [@Hayden\_Smith](https://www.ruby-forum.com/u/Hayden_Smith)\
**Post date:** [March 16, 2009, 12:25pm UTC](https://www.ruby-forum.com/t/rudamentary-question/162980/1 "2009-03-16T12:25:19Z")

</div>

Greetings,  
I’m looking to write a script to perform the functions listed as below,  
would anyone have any ideas as to how I might approach this situation:

1. I have a file called “David Slater - Pensive Moments.txt” which I  
wish to format to look like “David\_Slater\_-\_Pensive\_Moments.txt”:  
Therefore translating all spaces for underscores.
2. I have a file called “[01] I am a fish.mp3” which I want to look like  
“01-I\_Am\_A\_Fish.mp3” thus removing open brackets around numbers at the  
beginning, replacing the space between the number and name with a dash  
and performing the listed function as above.
3. The listed functions are required to be performed recursively across  
directory trees.  
Any assistance people can provide would be most appreciated.  
Regards,  
Hayden Smith

* * *

Hayden Smith  
Email - removed\_email\_address@domain.invalid  
website - [http://www.AlternateProjects.com](http://www.AlternateProjects.com)  
Blog - [http://SolaceOfTheMind.BlogSpot.com](http://SolaceOfTheMind.BlogSpot.com)  
Phone–  
Australia:  
Gold Coast - +617-56-678-016  
Melbourne - +613-86-836-894  
Sydney - +612-80-617-305  
United States:  
+1-310-405-6408 (with extension 61756678016)  
United Kingdom:  
+44-292-000-7380 (with extension 61756678016)

---

<div class="post-metadata">

**Author:** ![Hayden\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cc9497/32.png) [@Hayden\_Smith](https://www.ruby-forum.com/u/Hayden_Smith)\
**Post date:** [March 16, 2009, 12:47pm UTC](https://www.ruby-forum.com/t/rudamentary-question/162980/2 "2009-03-16T12:47:39Z")

</div>

Hayden Smith wrote:

> Greetings,  
> I’m looking to write a script to perform the functions listed as below, would anyone have any ideas as to how I might approach this situation:
> 
> 1. I have a file called “David Slater - Pensive Moments.txt” which I wish to format to look like “David\_Slater\_-\_Pensive\_Moments.txt”: Therefore translating all spaces for underscores.
> 2. I have a file called “[01] I am a fish.mp3” which I want to look like “01-I\_Am\_A\_Fish.mp3” thus removing open brackets around numbers at the beginning, replacing the space between the number and name with a dash and performing the listed function as above.
> 3. The listed functions are required to be performed recursively across directory trees.  
> Any assistance people can provide would be most appreciated.  
> Regards,  
> Hayden Smith

Here are some places to start:

[http://ruby-doc.org/core/classes/Dir.html#M002347](http://ruby-doc.org/core/classes/Dir.html#M002347)  
[http://ruby-doc.org/core/classes/String.src/M000832.html](http://ruby-doc.org/core/classes/String.src/M000832.html)  
[http://ruby-doc.org/core/classes/File.src/M002560.html](http://ruby-doc.org/core/classes/File.src/M002560.html)

-Justin

---

<div class="post-metadata">

**Author:** ![Hayden\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cc9497/32.png) [@Hayden\_Smith](https://www.ruby-forum.com/u/Hayden_Smith)\
**Post date:** [March 16, 2009, 1:02pm UTC](https://www.ruby-forum.com/t/rudamentary-question/162980/3 "2009-03-16T13:02:55Z")

</div>

You should be able to make something out of:

- Dir#glob ([class Dir - RDoc Documentation](http://www.ruby-doc.org/core/classes/Dir.html#M002347))
- String#sub  
([class String - RDoc Documentation](http://www.ruby-doc.org/core/classes/String.html#M000831))
- String#gsub  
([class String - RDoc Documentation](http://www.ruby-doc.org/core/classes/String.html#M000832))

The code is left as an exercise for the reader. 🙂

---

<div class="post-metadata">

**Author:** ![Hayden\_Smith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cc9497/32.png) [@Hayden\_Smith](https://www.ruby-forum.com/u/Hayden_Smith)\
**Post date:** [March 16, 2009, 1:56pm UTC](https://www.ruby-forum.com/t/rudamentary-question/162980/4 "2009-03-16T13:56:26Z")

</div>

Hayden Smith schrieb:

> Email - removed\_email\_address@domain.invalid  
> +44-292-000-7380 (with extension 61756678016)  
> I would use a construct like this to replace the parts:  
> string.gsub(/^[(\d+)]\s?/,  
> ‘\1-’).gsub(/\s+([-+\w])/){"\_#{$1.upcase}"}.gsub(/^-/, ‘’)  
> where string is your filename (works for both versions ;))
