views:

241

answers:

3

In Perl (on Windows) how do I determine the last modified time of a directory?

Note:

 opendir my($dirHandle), "$path";
 my $modtime = (stat($dirHandle))[9];

results in the following error:

The dirfd function is unimplemented at scriptName.pl line lineNumber.

+3  A: 

Use the Win32::UTCFileTime module on CPAN, which mirrors the built-in stat function's interface:

use Win32::UTCFileTime qw(:DEFAULT $ErrStr);
@stats = stat $file or die "stat() failed: $ErrStr\n";
Ether
+1  A: 

Apparently the real answer is just call stat on a path to the directory (not on a directory handle as many examples would have you believe) (at least for windows).

example:

my $directory = "C:\\windows";
my @stats = stat $directory;
my $modifiedTime = $stats[9];

if you want to convert it to localtime you can do:

my $modifiedTime = localtime $stats[9];

if you want to do it all in one line you can do:

my $modifiedTime = localtime((stat("C:\\Windows"))[9]);

On a side note, the Win32 UTCFileTime perl module has a syntax error which prevents the perl module from being interpreted/compiled properly. Which means when it's included in a perl script, that script also won't work properly. When I merge over all the actual code that does anything into my script and retry it, Perl eventually runs out of memory and execution halts. Either way there's the answer above.

bob
Did you file a bug on CPAN about this issue?
Ether
A: 
 my $dir_path = "path_of_your_directory";
 my $mod_time =  ( stat ( $dir_path ) )[9];
Shalini