search latest version of file in directory

M

Mmcolli00 Mom

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
 
R

Robert Klemme

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
 
W

William James

Mmcolli00 said:
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
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top