From: Ben Giddings Date: 2007-10-16T06:38:57+09:00 Subject: Re: pulling a file with today's date On Monday 15 October 2007, Peter Bailey wrote: > Hi, > Can someone please help me to parse a file by its date so that, when I > ftp to a site, I only pull those files that are dated with today's date, > nothing else? I'm OK with pulling all files over, looking at them on my > side of the fence, and then just discarding those files that aren't > timestamped with today's date. When you say "parse a file", do you mean parse the contents of the file, or simply select a specific file? I think you're talking about choosing a file based on the filename matching a date, but I'm not sure. The answer really depends on the format of the date. If it's a nice iso8601 format date string (i.e. YYYY-MM-DD) it's a little easier than other formats, but none of them is really hard. Basically all you have to do is create a regular expression object that will match the pattern you're looking for. You create the regexp based off a date string that comes from today's date For example, if the file is 2007-10-15-secretdata.zip: First create a string with the current date formatted however the date in the filename is formatted: date_str = Time.new.strftime("%Y-%m-%d") Next create a regular expression based on that string: file_match_regex = Regexp.new("^" + date_str + "-secretdata.zip$") Finally match the filenames against that regular expression: irb(main):008:0> file_match_regex.match("2007-10-15-secretdata.zip") => # irb(main):009:0> file_match_regex.match("2007-10-16-secretdata.zip") => nil If you want to do this on the remote files, you'll probably want to use Ruby's FTP libraries. If you want to do it after you transfer the files over, you can transfer the file and then check them based on whatever matches the regexp. Ben