Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Need ruby code to get a latest file from a Directory

Reply
Thread Tools

Need ruby code to get a latest file from a Directory

 
 
Rajeswar reddy Gaulla
Guest
Posts: n/a
 
      08-18-2008
Hi All,

I am having log files with time stamp in a log directory, how to open a
latest log file and read it?.

Eg:
directory name log/
files
log/log20080812152634Lbuild.34.xml
log/log20080812122634Lbuild.34.xml
log/log20080812102634Lbuild.34.xml
log/log20080811142634Lbuild.34.xml
log/log20080811122634Lbuild.34.xml

If anybody know the solution, please let me know.

With Regards
GRR
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Nathan Powell
Guest
Posts: n/a
 
      08-18-2008
On Tue, Aug 19, 2008 at 02:23:07AM +0900, Rajeswar reddy Gaulla wrote:
> I am having log files with time stamp in a log directory, how to open a
> latest log file and read it?.


Dir.entries('.').sort_by {|f| File.mtime(f)}.reverse[0]

Does that do what you want?

--
nathan
nathan_at_nathanpowell_dot_org

If all else fails, immortality can always be assured by spectacular error.
~ John Kenneth Galbraith
------------------------------------

 
Reply With Quote
 
 
 
 
brabuhr@gmail.com
Guest
Posts: n/a
 
      08-18-2008
On Mon, Aug 18, 2008 at 1:23 PM, Rajeswar reddy Gaulla
<(E-Mail Removed)> wrote:
> I am having log files with time stamp in a log directory, how to open a
> latest log file and read it?.
>
> Eg:
> directory name log/
> files
> log/log20080812152634Lbuild.34.xml
> log/log20080812122634Lbuild.34.xml
> log/log20080812102634Lbuild.34.xml
> log/log20080811142634Lbuild.34.xml
> log/log20080811122634Lbuild.34.xml
>
> If anybody know the solution, please let me know.


Maybe something like:

newest_file = Dir.entries(Dir:wd).sort_by{|e|
# assuming that the files are always log + TIMESTAMP + L:
scan(/log\d{14}L/)[0]
}.last

File.open(newest_file, "r") do |f|
#...
end

?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
search latest version of file in directory Mmcolli00 Mom Ruby 2 02-10-2009 08:49 AM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Get the latest file in a directory steve.chambers@gmail.com Java 4 07-19-2006 05:40 PM
Displaying latest file in a directory maxhauser@mailcity.com ASP General 1 06-24-2005 05:26 PM
get the latest file in a directory Hank Python 3 10-30-2003 09:07 AM



Advertisments