如何在 Windows 上的 Perl 中获取目录的最后修改时间?

发布于 2024-08-29 04:53:20 字数 248 浏览 3 评论 0原文

在 Perl 中(在 Windows 上)如何确定目录的上次修改时间?

注意:

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

导致以下错误:

dirfd 函数在 scriptName.pl 行 lineNumber 处未实现。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吐个泡泡 2024-09-05 04:53:20

显然,真正的答案只是在目录路径上调用 stat (而不是像许多示例让您相信的那样在目录句柄上调用 stat )(至少对于 Windows)。

例如:

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

如果您想将其转换为本地时间,您可以这样做:

my $modifiedTime = localtime $stats[9];

如果您想在一行中完成所有操作,您可以这样做:

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

附带说明,Win32 UTCFileTime perl 模块有一个语法错误,该错误会阻止解释 perl 模块/编译正确。这意味着当它包含在 Perl 脚本中时,该脚本也将无法正常工作。当我将执行任何操作的所有实际代码合并到我的脚本中并重试时,Perl 最终会耗尽内存并停止执行。不管怎样,上面已经有答案了。

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.

暗恋未遂 2024-09-05 04:53:20
 my $dir_path = "path_of_your_directory";
 my $mod_time =  ( stat ( $dir_path ) )[9];
 my $dir_path = "path_of_your_directory";
 my $mod_time =  ( stat ( $dir_path ) )[9];
┾廆蒐ゝ 2024-09-05 04:53:20

使用 CPAN 上的 Win32::UTCFileTime 模块,该模块镜像 内置统计函数的接口:

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

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文