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'));
####
foo.txt Last Accessed: 07/18/2002 at 12:21:04
####
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_READ(),
[], OPEN_EXISTING() , 0, [])
or die "Cannnot read file".$self->{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", "FileTimeToLocalFileTime",
['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", "FileTimeToSystemTime",
['P','P'] , 'I');
$FileToSystem->Call($localfiletime,$systemtime);
my($year,$month,$wday,$day,$hour,$minute,$second,$msecond)
= unpack("SSSSSSSS",$systemtime);
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,$msecond);
}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, create, 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
my $filetime = Win32::Filetime->new($filepath);
This is a constructor for a new Win32::Filetime object. It takes an absolute
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
Returns an array corresponding to the Created time of the file
=item B
Returns an array corresponding to the Modified time of the file
=item B
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;