Hmm, well they're named nicely, so you can probably do something like
File.open(Dir['some_file.*.csv'].sort.last) do |f|
puts f.read # print out the contents of f
end
if you're confident that the dates in the names are accurate.
If you want to be more pedantic, you can check by the file's ctime
(which is when the directory's info for that file was changed) or
mtime (which is when the file itself was last changed.
latest = Dir['some_file.*.csv'].sort_by { |f| File.ctime(f) }.last
(Use Dir.glob instead if you need to). That's basically just sorting
the filenames based on the file's timestamp (ascending), and taking
the last one.
JJ
2008/6/21 Justin To <tekmc / hotmail.com>:
> Thanks for the detailed explanation Bryan. I've figured out how to use
> it now, but I'm still stuck on how to check for the latest file:
>
> For instance,
>
> some_file.2008-06-02.csv
> some_file.2008-06-03.csv
>
> Now I want to traverse the folder and open up the latter file. If it is
> a viable solution, is there a command that returns the most recent file
> with the name pattern "some_file.\d{4}-\d{2}-\d{2}.csv"
>
> Thanks so much!
> --
> Posted via http://www.ruby-forum.com/.
>
>
--
JJ