tags:

views:

29

answers:

2

How to get 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?

A: 

I believe File.ctime is what you are looking for.

The documentation says that the change refer to the change in directory information, which is sort of the same as creation time I would think.

Returns the change time for the named file (the time at which directory information about the file was changed, not the file itself). http://ruby-doc.org/core/classes/File.html

Henrik
+2  A: 

Apparently, the "ctime" (creation time) metadata attribute of a file is system dependent as some systems store the time that a file was created (its "birth date") and others 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.
maerics