Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 751
bob Avatar asked Dec 23 '22 03:12

bob


2 Answers

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.

like image 141
bob Avatar answered Jan 04 '23 10:01

bob


 my $dir_path = "path_of_your_directory";
 my $mod_time =  ( stat ( $dir_path ) )[9];
like image 22
Shalini Avatar answered Jan 04 '23 10:01

Shalini