PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > file renaming/parsing
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
file renaming/parsing

Réponse
 
LinkBack Outils de la discussion
Vieux 02/12/2007, 08h44   #1
Mike Mr.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut file renaming/parsing

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

  Réponse avec citation
Vieux 02/12/2007, 10h52   #2
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: file renaming/parsing

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])


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

  Réponse avec citation
Vieux 02/12/2007, 11h13   #3
Axel Etzold
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: file renaming/parsing


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


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


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.
(http://ruby-doc.org/core/classes/Time.html#M000261)

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|
file_name.sub!(/\.[^\.]*$/,'') # chop off endings
month.each{|m| if file_name.index(m)!=nil; file_month=m; break; end}
if file_month==''
raise "No month given for file : " + file_name
end
ref_year=year.match(file_name)
if ref_year!=nil
file_year=ref_year[0]
else
raise "No year given for file : " + file_name
end
file_name.sub!(ref_year[0],'') # remove year, so that doesn't interfere with search for file_name's day
ref_day=day.match(file_name)
if ref_day!=nil
file_day=ref_day[0]
else
raise "No day given for file : " + file_name
end
# get day of week
dow=Time.parse([file_month,file_day,file_year].join(' ')).wday
result<<Time.parse([file_month,file_day,file_year].join(' ')).to_s
}

p 'result'
p result


--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

  Réponse avec citation
Vieux 02/12/2007, 23h18   #4
markonlinux@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: file renaming/parsing

Hi,

On Dec 2, 7:44 pm, "Mike Mr." <saltydog4...@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])
> --
> Posted viahttp://www.ruby-forum.com/.



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

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,

--
Mark
  Réponse avec citation
Vieux 03/12/2007, 21h03   #5
Mike M.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: file renaming/parsing

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h30.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14355 seconds with 13 queries