# File renaming/parsing help

**URL:** <https://rubytalk.org/t/file-renaming-parsing-help/42670>\
**Category:** ruby-talk\
**Created:** [2 December 2007 08:44 UTC](https://rubytalk.org/t/file-renaming-parsing-help/42670 "2007-12-02T08:44:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_M2](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Mike\_M2](https://rubytalk.org/u/Mike_M2)\
**Post date:** [2 December 2007 08:44 UTC](https://rubytalk.org/t/file-renaming-parsing-help/42670/1 "2007-12-02T08:44:56Z")

</div>

First of all let me say that I am an absolute newb when it comes to  
rails. I am simply trying to work on an existing script that was  
written by someone else.

I need to take a filename that will always have a similar name and turn  
it into an rfc2822 compliant line for an xml file. Here is what I have  
so far but I am absolutely lost as you can see. sad

The filenames will always be the same, e.g. November 15,2007.mp3 or  
September 7,2008.mp3, etc. One thing I am concerned about is the day  
since sometimes it will be a single digit and sometimes a double digit,  
although I am sure their is a way around that too.

I would love if someone could shed some light on this for me. I might  
even learn something in the process. smile

#The goal is turn "November 15,2007.mp3" into "Wed, 15 Nov 2007 19:00:00  
GMT"

> **···**
>
> #  
> #So far the output below gives me Wed, Nov15
> 
> filename = "November 15, 2007.mp3"  
> puts "Wed, " + ( filename[0,3]) + ( filename[-12,2])  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [2 December 2007 10:52 UTC](https://rubytalk.org/t/file-renaming-parsing-help/42670/2 "2007-12-02T10:52:31Z")

</div>

You could make your life easy by using a Date object and the #parse method...

str = "November 15, 2007.mp3"  
date\_str = str.split('.')[0]  
d = Date.parse(date\_str) ##### parse it  
puts d.strftime "%a, %e %b %Y.mp3" ##### output it the way you want

You can add other formats too (see #strftime and look at the source)  
Todd

> **···**
>
> On Dec 2, 2007 2:44 AM, Mike Mr. \<saltydog4791@gmail.com\> wrote:
> 
> > First of all let me say that I am an absolute newb when it comes to  
> > rails. I am simply trying to work on an existing script that was  
> > written by someone else.
> > 
> > I need to take a filename that will always have a similar name and turn  
> > it into an rfc2822 compliant line for an xml file. Here is what I have  
> > so far but I am absolutely lost as you can see. sad
> > 
> > The filenames will always be the same, e.g. November 15,2007.mp3 or  
> > September 7,2008.mp3, etc. One thing I am concerned about is the day  
> > since sometimes it will be a single digit and sometimes a double digit,  
> > although I am sure their is a way around that too.
> > 
> > I would love if someone could shed some light on this for me. I might  
> > even learn something in the process. smile
> > 
> > #The goal is turn "November 15,2007.mp3" into "Wed, 15 Nov 2007 19:00:00  
> > GMT"  
> > #  
> > #So far the output below gives me Wed, Nov15
> > 
> > filename = "November 15, 2007.mp3"  
> > puts "Wed, " + ( filename[0,3]) + ( filename[-12,2])

---

<div class="post-metadata">

**Author:** ![Axel\_Etzold](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Axel\_Etzold](https://rubytalk.org/u/Axel_Etzold)\
**Post date:** [2 December 2007 11:13 UTC](https://rubytalk.org/t/file-renaming-parsing-help/42670/3 "2007-12-02T11:13:39Z")

</div>

-------- Original-Nachricht --------

> Datum: Sun, 2 Dec 2007 17:44:56 +0900  
> Von: "Mike Mr." \<saltydog4791@gmail.com\>  
> An: ruby-talk@ruby-lang.org  
> Betreff: file renaming/parsing help

> First of all let me say that I am an absolute newb when it comes to  
> rails. I am simply trying to work on an existing script that was  
> written by someone else.
> 
> I need to take a filename that will always have a similar name and turn  
> it into an rfc2822 compliant line for an xml file. Here is what I have  
> so far but I am absolutely lost as you can see. sad
> 
> The filenames will always be the same, e.g. November 15,2007.mp3 or  
> September 7,2008.mp3, etc. One thing I am concerned about is the day  
> since sometimes it will be a single digit and sometimes a double digit,  
> although I am sure their is a way around that too.
> 
> I would love if someone could shed some light on this for me. I might  
> even learn something in the process. smile
> 
> #The goal is turn "November 15,2007.mp3" into "Wed, 15 Nov 2007 19:00:00  
> GMT"  
> #  
> #So far the output below gives me Wed, Nov15
> 
> filename = "November 15, 2007.mp3"  
> puts "Wed, " + ( filename[0,3]) + ( filename[-12,2])  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

Dear Mike,

here's some code using regexps .. that avoids knowing  
the exact indices where your one-digit or two-digit day information is.  
To get the day of week, you can use the time class of Ruby.  
([class Time - RDoc Documentation](http://ruby-doc.org/core/classes/Time.html#M000261%5C))

Best regards,

Axel

> **···**
>
> ------------------------------------------------------------  
> require "time"
> 
> mp\_files=["November 15, 2007.mp3"]  
> result=  
> month=["January","February","March","April","May","June","July","August","September","October","November","December"]  
> year=/(1|2)[0-9]{3}/ # year must start in 1 or in 2 and have 4 digits  
> day=/[0-9]{1,2}/
> 
> file\_month=''  
> file\_year=''  
> file\_day=''  
> mp\_files.each{|file\_name|  
> &nbsp;&nbsp;file\_name.sub!(/\.[^\.]\*$/,'') # chop off endings  
> &nbsp;&nbsp;month.each{|m| if file\_name.index(m)!=nil; file\_month=m; break; end}  
> &nbsp;&nbsp;if file\_month==''  
> &nbsp;&nbsp;&nbsp;&nbsp;raise "No month given for file : " + file\_name  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;ref\_year=year.match(file\_name)  
> &nbsp;&nbsp;if ref\_year!=nil  
> &nbsp;&nbsp;&nbsp;&nbsp;file\_year=ref\_year[0]  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;raise "No year given for file : " + file\_name  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;file\_name.sub!(ref\_year[0],'') # remove year, so that doesn't interfere with search for file\_name's day   
> &nbsp;&nbsp;ref\_day=day.match(file\_name)  
> &nbsp;&nbsp;if ref\_day!=nil  
> &nbsp;&nbsp;&nbsp;&nbsp;file\_day=ref\_day[0]  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;raise "No day given for file : " + file\_name  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;# get day of week  
> &nbsp;&nbsp;dow=Time.parse([file\_month,file\_day,file\_year].join(' ')).wday  
> &nbsp;&nbsp;result\<\<Time.parse([file\_month,file\_day,file\_year].join(' ')).to\_s  
> }   
> &nbsp;&nbsp;  
> p 'result'  
> p result
> 
> --  
> Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten  
> Browser-Versionen downloaden: [GMX Browser - verwenden Sie immer einen aktuellen Browser. Kostenloser Download.](http://www.gmx.net/de/go/browser)

---

<div class="post-metadata">

**Author:** ![Mark\_Woodward](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@Mark\_Woodward](https://rubytalk.org/u/Mark_Woodward)\
**Post date:** [2 December 2007 23:20 UTC](https://rubytalk.org/t/file-renaming-parsing-help/42670/4 "2007-12-02T23:20:01Z")

</div>

Hi,

> First of all let me say that I am an absolute newb when it comes to  
> rails. I am simply trying to work on an existing script that was  
> written by someone else.
> 
> I need to take a filename that will always have a similar name and turn  
> it into an rfc2822 compliant line for an xml file. Here is what I have  
> so far but I am absolutely lost as you can see. sad
> 
> The filenames will always be the same, e.g. November 15,2007.mp3 or  
> September 7,2008.mp3, etc. One thing I am concerned about is the day  
> since sometimes it will be a single digit and sometimes a double digit,  
> although I am sure their is a way around that too.
> 
> I would love if someone could shed some light on this for me. I might  
> even learn something in the process. smile
> 
> #The goal is turn "November 15,2007.mp3" into "Wed, 15 Nov 2007 19:00:00  
> GMT"  
> #  
> #So far the output below gives me Wed, Nov15
> 
> filename = "November 15, 2007.mp3"  
> puts "Wed, " + ( filename[0,3]) + ( filename[-12,2])  
> --  
> Posted viahttp://www.ruby-forum.com/.

not exactly what you're after (not sure how you intend to generate  
times?) but might help.

require 'date'  
require 'time'

str = "November 15, 2007.mp3"  
#str\_sans\_ext = str.split('.')[0]  
# or,  
str\_sans\_ext = str.gsub(/\.mp3/,'')  
new\_date = Time.parse(str\_sans\_ext)  
puts new\_date  
puts new\_date.httpdate

cheers,

> **···**
>
> On Dec 2, 7:44 pm, "Mike Mr." \<saltydog4...@gmail.com\> wrote:
> 
> --  
> Mark

---

<div class="post-metadata">

**Author:** ![Mike\_M2](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Mike\_M2](https://rubytalk.org/u/Mike_M2)\
**Post date:** [3 December 2007 21:03 UTC](https://rubytalk.org/t/file-renaming-parsing-help/42670/5 "2007-12-03T21:03:00Z")

</div>

Todd Benson wrote:

> **···**
>
> > On Dec 2, 2007 2:44 AM, Mike Mr. \<saltydog4791@gmail.com\> wrote:
> > 
> > > since sometimes it will be a single digit and sometimes a double digit,
> > > 
> > > filename = "November 15, 2007.mp3"  
> > > puts "Wed, " + ( filename[0,3]) + ( filename[-12,2])
> > 
> > You could make your life easy by using a Date object and the #parse  
> > method...
> > 
> > str = "November 15, 2007.mp3"  
> > date\_str = str.split('.')[0]  
> > d = Date.parse(date\_str) ##### parse it  
> > puts d.strftime "%a, %e %b %Y.mp3" ##### output it the way you want
> > 
> > You can add other formats too (see #strftime and look at the source)  
> > Todd
> 
> Thank you so much Todd. I integrated your code successfully and it  
> works great. Kudos to all of you who lent me a hand.
> 
> Warm regards,
> 
> Mike
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
