search latest version of file in directory

Discussion in 'Ruby' started by Mmcolli00 Mom, Feb 9, 2009.

  1. 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/.
    Mmcolli00 Mom, Feb 9, 2009
    #1
    1. Advertising

  2. 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
    Robert Klemme, Feb 9, 2009
    #2
    1. Advertising

  3. 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
    William James, Feb 10, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    6,750
    Roland de Ruiter
    Jul 19, 2006
  2. Kali K E

    Copying latest version of the file

    Kali K E, Sep 12, 2003, in forum: Python
    Replies:
    0
    Views:
    249
    Kali K E
    Sep 12, 2003
  3. Hank
    Replies:
    3
    Views:
    379
    Tim Golden
    Oct 30, 2003
  4. V Green
    Replies:
    0
    Views:
    822
    V Green
    Feb 5, 2008
  5. PA Bear [MS MVP]
    Replies:
    0
    Views:
    927
    PA Bear [MS MVP]
    Feb 5, 2008
Loading...

Share This Page