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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Win32::Filetime
by Ovid (Cardinal) on Jul 18, 2002 at 23:53 UTC | |
|
Re: Win32::Filetime
by demerphq (Chancellor) on Jul 18, 2002 at 21:39 UTC |