How can I get the file creation time in Ruby on Windows?
File.ctime
is supposed to return change time.
dir /tc
in cmd.exe returns creation time with bunch of other stuff.
Is there a better way than exec'ing it and parsing?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With