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

Apparently under *NIX the create time of a file is not stored (as ferrency pointed on CB out the meaning of this time is a bit subjective.)

Nevertheless MS does store this information, but I cant seem to find a way to access it short of farming out to a "dir" in backticks.

Admittadly I havent had time to search the MS docs for the API call and make my own wrapper but I was thinking that there must be some module that does this?

Thanks in advance,

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.

  • Comment on How to find the create time of a file under MS

Replies are listed 'Best First'.
Re: How to find the create time of a file under MS
by jsprat (Curate) on Jul 16, 2002 at 15:33 UTC
    stat[10] gives you the creation time of the file (on Win32, at least), while the output of dir gives you the last modified time. Try the following and compare it to the file properties in explorer:
    #!/usr/bin/perl -w use strict; #use diagnostics; use warnings; use POSIX; my $file = $ARGV[0] || 'a.pl'; my @t = stat($file); print "Accessed: ", strftime("%A, %B %d, %Y, %H:%M:%S",localtime($t[8] +)), "\n"; print "Modified: ", strftime("%A, %B %d, %Y, %H:%M:%S",localtime($t[9] +)), "\n"; print "Created: ", strftime("%A, %B %d, %Y, %H:%M:%S",localtime($t[10] +)), "\n";

    Update: added dir and last modified above, emphasized 'on Win32'

    Update 2.5: Okay, thunders made me curious. I dug into the perl source. Perl's stat (built with Visual C) is implemented using the stat or wstat function. According to MS, these functions return st_ctime, which is the "Time of creation of file. Valid on NTFS but not on FAT formatted disk drives." This is what stat[10] returns.

    Also, if you move a file across file systems (from c: to d:, for example) the creation time changes.

      stat[10] is ctime which is literally inode change time. Thing is, in my test at least on Windows this appears to return creation time. On Unix something as simple as moving a file will update this value, as will security related changes like chgrp. not the case on Windows, the value stays the same, and appears to be the actual file creation time.
      Thanks very much.

      Im going to write to p5p to see if this is considered a feature and if so ill provide them a patch for the documentation.

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

      How can I SET the values discussed here using Perl, e.g. if I move a file from one partition to another but want it to have the original (file's) time stamp?
Re: How to find the create time of a file under MS
by perrin (Chancellor) on Jul 16, 2002 at 16:49 UTC
    Win32::File from libwin32 looks like a good place to start.
Re: How to find the create time of a file under MS
by screamingeagle (Curate) on Jul 16, 2002 at 17:08 UTC
    The API's you're looking for are the FindFirstFile and the FindNextFile APIs, which return, the created, last-accessed and last-modified date. To use these, you could use the Win32::API module.
Re: How to find the create time of a file under MS
by thunders (Priest) on Jul 17, 2002 at 16:14 UTC
    Here it is with calls to the Win32 API. I don't know if this is any more accurate, but it worked fine on my system.
    #!/usr/bin/perl -w use strict; use Win32::API; use Win32API::File (qw/:ALL/); my $filename = "file.txt"; my $GetFileTime = new Win32::API("kernel32", "GetFileTime", ['N', 'P', + 'P', 'P' ], 'I'); my $hFile= CreateFile( $filename, GENERIC_READ(), FILE_SHARE_READ(), [], OPEN_EXISTING() , 0, []) or die "Can't re +ad file $filename"; my %filetime =( create=>undef,access=>undef,write=>undef ); for (keys %filetime) { $filetime{$_} = pack("LL", 0, 0); } $GetFileTime->Call($hFile,$filetime{'create'},$filetime{'access'},$fil +etime{'write'}); for (keys %filetime) { my $filetimepointer = $filetime{$_}; my $systemtime = pack("SSSSSSSS",0,0,0,0,0,0,0,0); my $FileToSystem = new Win32::API("kernel32", "FileTimeToSyste +mTime", ['P','P'] , 'I'); $FileToSystem->Call($filetimepointer,$systemtime); my($year,$month,$weekday,$day,$hour,$minute,$second,$msecond) += unpack("SSSSSSSS",$systemtime); printf ("$_ time:\t %d/%02d/%02d %02d:%02d:%02d", $month, $day +, $year ,$hour,$minute,$second); print "\n"; }
      Wow.

      Thanks a lot thunders!

      +++++

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.