How to Order Files by Last Modified Time in Ruby

How to order files by last modified time in ruby?

How about simply:

# If you want 'modified time', oldest first
files_sorted_by_time = Dir['*'].sort_by{ |f| File.mtime(f) }

# If you want 'directory change time' (creation time for Windows)
files_sorted_by_time = Dir['*'].sort_by{ |f| File.ctime(f) }

Is it possible to read a file's modification date with Ruby?

Use mtime:

File.mtime("testfile")
=> 2014-04-13 16:00:23 -0300

Find date file was last modified

You can pass it a string of the file name.

irb(main):001:0> File.mtime("Gemfile")
=> 2016-08-22 13:54:43 -0700

To reference files from within rails you can use Rails.root.join:

gemfile = Rails.root.join("Gemfile")
=> #<Pathname:/Users/username/projects/appname/Gemfile>

File.mtime(gemfile)
=> 2016-08-22 13:54:43 -0700

The docs also mention you can pass it an IO object.

Sort List of Files Paths by Date Modified in Ruby

The line you have is working as you would expect. I've created four files and this is the output by ls -lt, which sorts the file by modified time:

$ ls -t
2 3 4 1

Your example outputs:

@files = Dir.entries(Dir.pwd)
@files.sort_by { |file| File.mtime(file) }
=> ["2", ".", "3", "4", "1", ".."]

Note: By convention a method in any set does not change the set itself. You need to call sort_by! in order to apply the sorted set to the original set.

How do I organize files by creation date?

The creation time with Ruby on OS X is not available through the File API. One way is shelling out to stat(1). Not pretty but does at least return the creation (a.k.a birth) time:

def birth(file)
Time.at(`stat -f%B "#{file}"`.chomp.to_i)
end

Dir.entries('.').sort_by {|f| birth f }

Or use the partition answer given.

Here's a detailed post on a common misconception: ctime does not mean creation time.

Create array of files and sort by date in ruby

You can use the sort_by method in conjunction with File.mtime method, which returns the last modification time of the given file.

filenames.sort_by {|filename| File.mtime(filename) }

How to get last modified file in a directory to pass to system commands using Ruby?

There's no need to shell out to ls and parse its output at all. Ruby gives you standard library methods to fetch directory contents and examine file mtimes. Here's a ruby method to return the name of a file in a directory with the latest mtime.

def last_modified_in dir
Dir.glob( File.join( dir,'*' ) ).
select {|f| File.file? f }.
sort_by {|f| File.mtime f }.
last
end

irb> system 'mkdir -p /tmp/foo'
irb> system 'rm /tmp/foo/*'
irb> ('a'..'c').each { |f| system "touch /tmp/foo/#{f}"; sleep 1; }
irb> puts last_modified_in '/tmp/foo'
# => /tmp/foo/c

Sort order in `Dir.entries`

According to the Ruby language docs, Dir.entries() does not guarantee any particular order of the listed files, so if you require some order it's best to do it explicitly yourself.

For example, if you need to sort by file modification time (oldest to newest), you could do the following:

Dir.entries('.').sort_by { |x| File.mtime(x) }

In ruby, how might I find all Files between two dates

Perhaps something like this? (untested):

selected_files = Dir.glob("*.pdf").select do |file|
mtime = File.mtime(file)

# if in a rails environment:
# (1.day.ago .. Time.now).cover?(mtime)

# if not in rails environment but want to use that code do this before that line:
# require 'active_support/all'

# else do the math:
# mtime > (Time.now - 86400) and mtime < Time.now
end


Related Topics



Leave a reply



Submit