How can I get the last modified time of a directory in Perl on Windows?

bob picture bob · Apr 17, 2010 · Viewed 7k times · Source

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.

Answer

bob picture bob · Apr 17, 2010

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.