How do I get the file creation time in Ruby on Windows?
Apparently, the "ctime" ("creation" or "change" time) metadata attribute of a file is system dependent as some systems (e.g. Windows) store the time that a file was created (its "birth date") and others (Posix systems, e.g. Linux) track the time that it was last updated. Windows uses the ctime attribute as the actual creation time, so you can use the various ctime
functions in Ruby.
The File class has static and instance methods named ctime
which return the last modified time and File::Stat has an instance method (which differs by not tracking changes as they occur).
File.ctime("foo.txt") # => Sun Oct 24 10:16:47 -0700 2010 (Time)
f = File.new("foo.txt")
f.ctime # => Will change if the file is replaced (deleted then created).
fs = File::Stat.new("foo.txt")
fs.ctime # => Will never change, regardless of any action on the file.
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.
How can I set a file creation time with ruby on Mac OS?
There is a Ruby solution with the method utime
. But you will have to set modification time (mtime) and access time (atime) at once. If you want to keep access time you could use:
File.utime(File.atime(path), modification_time, path)
See Ruby core documentation as well.
How can I get the path for the last created file in a directory using Ruby?
I think this is fairly brief:
Dir.glob(File.join(path, '*.*')).max { |a,b| File.ctime(a) <=> File.ctime(b) }
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) }
Creating a folder with the current time as name
My guess is that there is a character in your time string that Windows doesn't allow in a directory name (your code works fine for me on my Ubuntu machine). Try formatting your time so that it's just numeric, and that'll probably work:
require 'Fileutils.rb'
time = Time.now.strftime("%Y%m%d%H%M%S")
FileUtils.cp_r "C:/somefolder", "D:/somefolder_backup_#{time}"
Related Topics
Can Nokogiri Use Single Quotes for Attributes on Saving Xml
How to Display a Regexp Output in an Alphabetical List
Set Site/User Fields in Activeresource
Rspec Matcher That Checks Collection to Include Item That Satisfies Lambda
Testing HTML5 File Upload with Capybara/Selenium Webdriver - Ruby
How to Create Two Routes in One Block in Grape
How to Build a Docker Image for a Ruby Project Without Build Tools
Mechanize and Ntlm Authentication
I Trying to Make a Code That Gives the User a Personal Number After They Have Made an User
Ruby ".Downcase! " and "Downcase" Confusion
Rails Adding Multiple Objects to an Empty Array
Official Expansion of ||= Conditional Assignment Operator