Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > search latest version of file in directory

Reply
Thread Tools

search latest version of file in directory

 
 
Mmcolli00 Mom
Guest
Posts: n/a
 
      02-09-2009
Hi, I have a directory full of files and want to get the newest version
of each out of the directory. The files look like below. Please tell me
if you know a way to pull out the latest version of each. Thanks

34_web1_no2_09202008.txt
34_web1_no2_09212008.txt <--want to pull out this latest version
34_web2_no3_10122008.txt
34_web2_no3_10132008.txt <--want to pull out this latest version

******snippet************************************* ******

newsortfile = Dir["C:/Status2/*.html"].map{|@f|
[@f,File.mtime(@f)]}.sort_by{|@i|@i[1]}
@NewestFile = @i[1]

Dir.entries("C:/Status2/").each do |filename|
if File.mtime("C:/Status2/"+filename) == @NewestFile then
puts filename
end
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      02-09-2009
On 09.02.2009 20:46, Mmcolli00 Mom wrote:
> Hi, I have a directory full of files and want to get the newest version
> of each out of the directory. The files look like below. Please tell me
> if you know a way to pull out the latest version of each. Thanks
>
> 34_web1_no2_09202008.txt
> 34_web1_no2_09212008.txt <--want to pull out this latest version
> 34_web2_no3_10122008.txt
> 34_web2_no3_10132008.txt <--want to pull out this latest version
>
> ******snippet************************************* ******
>
> newsortfile = Dir["C:/Status2/*.html"].map{|@f|
> [@f,File.mtime(@f)]}.sort_by{|@i|@i[1]}
> @NewestFile = @i[1]
>
> Dir.entries("C:/Status2/").each do |filename|
> if File.mtime("C:/Status2/"+filename) == @NewestFile then
> puts filename
> end
> end


This is how I'd approach this:

dir = "C:/Status2"
files = Hash.new {|h,k| h[k] = []}

Dir[File.join(dir, "*.txt")].each do |f|
files[f[/^\d+_web\d+_no\d+/]] << File.join(dir, f)
end

files.each do |k,v|
puts v.sort_by! {|f| File.mtime(f)}.last
end

Cheers

robert
 
Reply With Quote
 
 
 
 
William James
Guest
Posts: n/a
 
      02-10-2009
Mmcolli00 Mom wrote:

> Hi, I have a directory full of files and want to get the newest
> version of each out of the directory. The files look like below.
> Please tell me if you know a way to pull out the latest version of
> each. Thanks
>
> 34_web1_no2_09202008.txt
> 34_web1_no2_09212008.txt <--want to pull out this latest version
> 34_web2_no3_10122008.txt
> 34_web2_no3_10132008.txt <--want to pull out this latest version
>
> ******snippet************************************* ******
>
> newsortfile = Dir["C:/Status2/*.html"].map{|@f|
> [@f,File.mtime(@f)]}.sort_by{|@i|@i[1]}
> @NewestFile = @i[1]
>
> Dir.entries("C:/Status2/").each do |filename|
> if File.mtime("C:/Status2/"+filename) == @NewestFile then
> puts filename
> end
> end


rx = /^.*_no\d+/
puts DATA.readlines.sort_by{|x| [x[rx],x[-8,8],x] }.
inject(nil){|prev,x|
puts prev if prev && prev[ rx ] != x[ rx ] ; x }

__END__
34_web1_no2_12202007.txt
34_web1_no2_09202008.txt
34_web1_no2_09212008.txt
34_web2_no3_12122007.txt
34_web2_no3_10122008.txt
34_web2_no3_10132008.txt
 
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
Re: Where to get stand alone Dot Net Framework version 1.1, version2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? MowGreen [MVP] ASP .Net 5 02-09-2008 01:55 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? PA Bear [MS MVP] ASP .Net 0 02-05-2008 03:28 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? V Green ASP .Net 0 02-05-2008 02:45 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
Active Directory Search fails ("The directory service is unavailab ejcosta ASP .Net Security 2 10-08-2004 09:57 AM



Advertisments