Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the file creation time in Ruby on Windows?

Tags:

windows

ruby

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?

like image 207
niteria Avatar asked Oct 24 '10 16:10

niteria


1 Answers

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.
like image 50
maerics Avatar answered Sep 29 '22 06:09

maerics