Need ruby code to get a latest file from a Directory

  • Thread starter Rajeswar reddy Gaulla
  • Start date
R

Rajeswar reddy Gaulla

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
 
N

Nathan Powell

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

brabuhr

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::pwd).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

?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top