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

Hi there, I am trying to write a script that lets me retreive the modified details of a file on a win2000 system, i would like to keep it as simple as possible, i have tried examples on the net but they are unix and they don't seem to work, this is the closes i have come to getting it to work

use File::stat; @sta=stat($_); @date= localtime(@sta[8]); printf("%d-%d-%d\n",$date[4]+1,$date[3],$date[5]+1900);
this will display

1-1-1970

can anyone help?

Thanks in advance.
Richard Wallis

Replies are listed 'Best First'.
Re: win32 getting file modified details
by pfaut (Priest) on Jan 12, 2003 at 16:12 UTC

    @sta[8] should be $sta[8] which would have been pointed out to you if you had use warnings;. You should also use strict;.

    Since you used File::stat, perl's built-in stat function which returns an array has been replaced by a version that returns a blessed reference to an array. You can use this to access the elements of the array by name instead of by index. You could still use the array but you have to dereference first.

    Using File::stat

    use warnings; use strict; use File::stat; my $sta = stat($_); my @date = localtime($sta->atime); printf("%d-%d-%d\n",$date[4]+1,$date[3],$date[5]+1900);

    or without

    use warnings; use strict; my @sta = stat($_); my @date = localtime($sta[8]); printf("%d-%d-%d\n",$date[4]+1,$date[3],$date[5]+1900);

    Update: After re-reading your post, I notice that you wanted modification time, not last access time. For that, use mtime instead of atime or offset 9 instead of offset 8.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
      hi there pfaut, thanks very much, that worked a treat.
Re: win32 getting file modified details
by rob_au (Abbot) on Jan 12, 2003 at 22:56 UTC
    While you have received the answer above, I would also direct you to have a look at the Win32::FileTime module which also offers an interface to determining file stamp times - This code was originally posted to this site here by thunders.

    Using this module, your code could look something like this:

    use Win32::FileTime; my $filetime = Win32::FileTime->new( $filename ); printf( "%4d/%02d/%02d\n", $filetime->Modify( 'year', 'month', 'day' ) + );

    The difference between this module and the example using stat described above is that this module employs native Win32 API's to determine this information and handles the VT_FILETIME data type returned.

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000010101"))'