after noticing a couple of nodes regarding finding the various Filetime Markers (Modify, Create, Access) and dealing with the bizarre VT_FILETIME data type on Win32 Systems I decided to write a module to make this simple. now I'm aware of functions like stat and localtime, but I thought maybe we could all use another WTDI.

Usage and docs are including in the pod, feel free to comment, or ask for other features, that I neglected to include.

ladies and gentlemen Win32::Filetime take 1.

Sample code:

use Win32::Filetime; my $filepath = "foo.txt"; my $filetime = Win32::Filetime->new($filepath); printf("$filepath Last Accessed:\t%02d/%02d/%04d at %d:%02d:%02d", $filetime->Access('month','day','year', 'hour','minute','second'));

Output:

foo.txt Last Accessed: 07/18/2002 at 12:21:04

Module follows:

package Win32::Filetime; use strict; use Win32::API; use Win32API::File (qw/:ALL/); sub new{ my $self ={}; $self->{FILENAME} = $_[1]; $self->{HFILE} = CreateFile( $self->{FILENAME}, GENERIC_READ(), FILE_SHARE_REA +D(), [], OPEN_EXISTING() , 0, []) or die "Cannnot read file".$s +elf->{FILENAME}; my %filetime =( create=>undef,access=>undef,modify=>undef ); for (keys %filetime) { $filetime{$_} = pack("LL", 0, 0); } my $GetFileTime = new Win32::API( "kernel32", "GetFileTime" +, ['N', 'P', 'P', 'P' ], 'I' +); $GetFileTime->Call( $self->{HFILE}, $filetime{'create'}, $filetime{'access'}, $filetime{'modify'}); $self->{ACCESS} = $filetime{'access'}; $self->{CREATE} = $filetime{'create'}; $self->{MODIFY} = $filetime{'modify'}; bless($self); return($self); } sub Create{ my $self = shift; my @args = @_; return getTime($self->{CREATE},@args) } sub Access{ my $self = shift; my @args = @_; return getTime($self->{ACCESS},@args) } sub Modify{ my $self = shift; my @args = @_; return getTime($self->{MODIFY},@args) } sub getTime{ my ($filetimepointer,@args) = @_; my $localfiletime = pack("LL", 0, 0); my $FileToLocal = new Win32::API( "kernel32", "FileTimeToLo +calFileTime", ['P','P'] , 'I'); $FileToLocal->Call($filetimepointer,$localfiletime); my $systemtime = pack("SSSSSSSS",0,0,0,0,0,0,0,0); my $FileToSystem = new Win32::API( "kernel32", "FileTimeToS +ystemTime", ['P','P'] , 'I'); $FileToSystem->Call($localfiletime,$systemtime); my($year,$month,$wday,$day,$hour,$minute,$second,$msecond) = unpack("SSSSSSSS",$systemtim +e); my %filetimehash = ( year=>$year, month=>$month, wday=>$wday, day=>$day, hour=>$hour, minute=>$minute, second=>$second,msecond=>$msecond ); my @returnarray; if (! scalar @args) { return ($year,$month,$wday,$day,$hour,$minute,$second,$mse +cond); }else{ for (@args) { die "bad argument \'$_\' to Win32::FileTime" unless (defined $filetimehash{$_}); push @returnarray, $filetimehash{$_}; } } return @returnarray; } =pod =head1 NAME Win32::Filetime - Perl module for determining Win32 file access, creat +e, and write times =head1 SYNOPSIS use Win32::Filetime; my $filepath = "foo.txt"; my $filetime = Win32::Filetime->new($filepath); printf("$filepath Last Accessed:\t%02d/%02d/%04d at %d:%02d:%02d", $filetime->Access('month','day','year', 'hour','minute','second')); =head1 DESCRIPTION This Module is Designed to make it easy to obtain the modify, access, +and create time for files on Win32 systems. =head1 CONSTRUCTOR =item B<new Win32::Filetime (FILEPATH)> my $filetime = Win32::Filetime->new($filepath); This is a constructor for a new Win32::Filetime object. It takes an ab +solute or relative filepath(i.e. "C:/My Documents/filename", "filename", "folder\\filename" with either "/" or "\\" as directory separators) as + an argument. =head1 METHODS =item B<Create([ARGUMENTS])> Returns an array corresponding to the Created time of the file =item B<Modify([ARGUMENTS])> Returns an array corresponding to the Modified time of the file =item B<Access([ARGUMENTS])> Returns an array corresponding to the Accessed time of the file Arguments can be any of the following: =item B<'year'>: returns a four-digit representaion of the year the file was created =item B<'month'>: returns a digit between 1 and 12 representing the month =item B<'wday'> returns a digit between 0 and 6 representing the day of the week Sunday = 0, Monday =1, etc =item B<'day'> returns the day of the month =item B<'hour'> returns the hour part of the filetime =item B<'minute'> returns the minute part of the filetime =item B<'second'> returns the second part of the filetime =item B<'msecond'> returns the millisecond part of the filetime Arguments are returned in the order they were called. if no arguments are specified the entire array in the order above. =head1 VERSION 0.0.2 =head1 AUTHOR Frank Bardelli =head1 LICENSE This program may be distributed under the same license as Perl itself =cut 1;

In reply to Win32::Filetime by thunders

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.