sonic has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to find the creation date for a given file on Windows. Is there a better way to do this than the stat command?

Replies are listed 'Best First'.
Re: File creation date
by crabbdean (Pilgrim) on Mar 15, 2004 at 04:05 UTC
    As far as I'm aware, not really. But I'm curious, why would you want to? A little more info may help us direct you accordingly to an answer. You could try File::stat instead. Check out the module documentation for a synopsis. Other tips:

    ** in doing stats if you have to stat the same file twice assign the result to a variable for later use
    use File::stat; $st = stat($file) or die "No $file: $!";
    ... and use the $st for any other checks instead of calling stat($file) each time. The reason: each time you call "stat" its a disk IO, which slows things down.

    ** if you want to know the last time any file in a directory was modified do a stat on the directory. I should contain a modified time of the youngest file. It will save you having to stat the entire subdirectory tree.

    ** if you are stat'ing lots of files, considering for speed sake refining what you call stat on instead of just stat'ing everything. That is, reduce the amount of files you'll stat by using a search criteria to get rid of 80% of the files you don't need to stat (depending on your needs).

    That's all that comes to mind at moment.

    Dean
    The Funkster of Mirth
    Programming these days takes more than a lone avenger with a compiler. - sam
    RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers
      ... and use the $st for any other checks instead of calling stat($file) each time. The reason: each time you call "stat" its a disk IO, which slows things down.
      BTW, this can also be accomplished using the plain stats command by passing the special parameter _ for subsequent calls:

      ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat(_);
      will just grab whatever the last stat($filename) returned without doing unnecessary disc I/O.

      Another thing to point out is that Unix systems don't store the file creation time, their $ctime is inode change time, which is the time the file's meta data on disc last got changed. On Windoze (with the NT filesystem), this is indeed the file creation time, you are correct.

      --saintmike

      Dean, Thanks for the response. I especially appreciate your performance optimization tips. That hints at the reason I'm trying to use something other than stat. Stat provides me info I don't need and slows me down (although not by much since I'm already in the kernel and already copying to a buffer, copying the extra info would be fast since its consecutive on disk). Also, I was just curious. I thought TMTOWTDI.
Re: File creation date
by hawtin (Prior) on Mar 15, 2004 at 05:24 UTC

    Have you looked at the file test operators -M -A and -C?

    There is the strangeness about what they measure (the time in days that was true when your script started). But that can be adjusted using the $^T variable.

    I also have a nagging feeling that file creation dates on Windows have something weird about them

Re: File creation date
by iburrell (Chaplain) on Mar 15, 2004 at 22:17 UTC
    Your could use the Win32 API directly with the Win32::API module. GetFileTime gives the creation, last access, and last write times.

    However, I would wajer that it would be slower than the builtin stat function. You have the overhead of Win32::API versus the other stuff that goes on in stat(). The stat() builtin is going to call GetFileTime to get its information and is probably going to do it in native code.

Re: File creation date
by Anonymous Monk on Mar 15, 2004 at 03:55 UTC
    Guess?