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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2656519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:17:47  来源:igfitidea点击:

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

windowsperldirectorystat

提问by bob

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

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

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.

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

采纳答案by bob

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).

显然,真正的答案只是在目录路径上调用 stat (而不是在您相信的许多示例中的目录句柄上)(至少对于 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.

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

回答by Ether

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

在 CPAN 上使用Win32::UTCFileTime模块,它反映了内置 stat 函数的接口:

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

回答by Shalini

 my $dir_path = "path_of_your_directory";
 my $mod_time =  ( stat ( $dir_path ) )[9];