Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl:如何在目录中搜索文件类型,按修改日期排序_Perl_Sorting_Search - Fatal编程技术网

Perl:如何在目录中搜索文件类型,按修改日期排序

Perl:如何在目录中搜索文件类型,按修改日期排序,perl,sorting,search,Perl,Sorting,Search,目录: C:/test C:/test/A1/apples.txt Modified 1-12-2013 C:/test/B1/oranges.txt Modified 1-12-2014 C:/test/C1/bananas.txt Modified 1-12-2015 将根目录(C:/test)提供给perl脚本,它将找到最近修改的目录和文件 perl findfile.pl C:/test Answer: C:/test/C1/bananas.txt 我需要这个脚本在我无

目录:

C:/test
C:/test/A1/apples.txt    Modified 1-12-2013
C:/test/B1/oranges.txt   Modified 1-12-2014
C:/test/C1/bananas.txt   Modified 1-12-2015
将根目录(C:/test)提供给perl脚本,它将找到最近修改的目录和文件

perl findfile.pl C:/test
Answer: C:/test/C1/bananas.txt
我需要这个脚本在我无法控制的多台计算机上工作,因此我无法安装“File::Find”或任何其他库

我尝试使用windows命令dir:

my @txt_files = `dir C:/test /O:-D /s *.txt`;
foreach my $line (@txt_files)
{
    if($line =~ /\s+Directory of (\S+)/)
    {
        print "$1\n"; # gets the directory
    }
...
}
如果从其他驱动器Z:X:etc运行,则此脚本不起作用。
在此基础上有什么建议吗?或者一个完全不同的路径也会起作用。

使用
opendir
readdir
读取目录,使用
-X
(检查
perldoc-f-X
)检查上次修改的日期,如果是文件或目录,则使用regexp查找.txt文件。维护一个找到的目录队列,只需运行一个
循环,同时从中提取元素,直到其为空

use strict;
use warnings;

unless ($ARGV[0]) { die "No starting directory specified" }
my @dir_queue = $ARGV[0];
my $most_recent_name;
my $most_recent_time;

while (@dir_queue) {
    my $current_dir = shift(@dir_queue);
    unless (opendir(my $current_dir_dh, $current_dir)) {
        die "Can't open directory $current_dir: $!"
    } else {
        while (my $entry = readdir($current_dir_dh)) {
            if ($entry eq '.' or $entry eq '..') { next }
            my $entry_full_name = "$current_dir/$entry";
            if (-d $entry_full_name) { push @dir_queue, $entry_full_name; next }
            unless ($entry =~ /\.txt$/i) { next }
            if (!$most_recent_time or ($most_recent_time > -M $entry_full_name)) {
                $most_recent_time = -M $entry_full_name;
                $most_recent_name = $entry_full_name;
            }
        }
    }
}

if (defined $most_recent_name) {
    print "Most recently modified: $most_recent_name"
} else {
    print "No files found."
}

使用
opendir
readdir
读取目录,使用
-X
(check
perldoc-f-X
)检查上次修改的日期,如果是文件或目录,则使用regexp查找.txt文件。维护一个找到的目录队列,只需运行一个
循环,同时从中提取元素,直到其为空

use strict;
use warnings;

unless ($ARGV[0]) { die "No starting directory specified" }
my @dir_queue = $ARGV[0];
my $most_recent_name;
my $most_recent_time;

while (@dir_queue) {
    my $current_dir = shift(@dir_queue);
    unless (opendir(my $current_dir_dh, $current_dir)) {
        die "Can't open directory $current_dir: $!"
    } else {
        while (my $entry = readdir($current_dir_dh)) {
            if ($entry eq '.' or $entry eq '..') { next }
            my $entry_full_name = "$current_dir/$entry";
            if (-d $entry_full_name) { push @dir_queue, $entry_full_name; next }
            unless ($entry =~ /\.txt$/i) { next }
            if (!$most_recent_time or ($most_recent_time > -M $entry_full_name)) {
                $most_recent_time = -M $entry_full_name;
                $most_recent_name = $entry_full_name;
            }
        }
    }
}

if (defined $most_recent_name) {
    print "Most recently modified: $most_recent_name"
} else {
    print "No files found."
}

使用
opendir
readdir
读取目录,使用
-X
(check
perldoc-f-X
)检查上次修改的日期,如果是文件或目录,则使用regexp查找.txt文件。维护一个找到的目录队列,只需运行一个
循环,同时从中提取元素,直到其为空

use strict;
use warnings;

unless ($ARGV[0]) { die "No starting directory specified" }
my @dir_queue = $ARGV[0];
my $most_recent_name;
my $most_recent_time;

while (@dir_queue) {
    my $current_dir = shift(@dir_queue);
    unless (opendir(my $current_dir_dh, $current_dir)) {
        die "Can't open directory $current_dir: $!"
    } else {
        while (my $entry = readdir($current_dir_dh)) {
            if ($entry eq '.' or $entry eq '..') { next }
            my $entry_full_name = "$current_dir/$entry";
            if (-d $entry_full_name) { push @dir_queue, $entry_full_name; next }
            unless ($entry =~ /\.txt$/i) { next }
            if (!$most_recent_time or ($most_recent_time > -M $entry_full_name)) {
                $most_recent_time = -M $entry_full_name;
                $most_recent_name = $entry_full_name;
            }
        }
    }
}

if (defined $most_recent_name) {
    print "Most recently modified: $most_recent_name"
} else {
    print "No files found."
}

使用
opendir
readdir
读取目录,使用
-X
(check
perldoc-f-X
)检查上次修改的日期,如果是文件或目录,则使用regexp查找.txt文件。维护一个找到的目录队列,只需运行一个
循环,同时从中提取元素,直到其为空

use strict;
use warnings;

unless ($ARGV[0]) { die "No starting directory specified" }
my @dir_queue = $ARGV[0];
my $most_recent_name;
my $most_recent_time;

while (@dir_queue) {
    my $current_dir = shift(@dir_queue);
    unless (opendir(my $current_dir_dh, $current_dir)) {
        die "Can't open directory $current_dir: $!"
    } else {
        while (my $entry = readdir($current_dir_dh)) {
            if ($entry eq '.' or $entry eq '..') { next }
            my $entry_full_name = "$current_dir/$entry";
            if (-d $entry_full_name) { push @dir_queue, $entry_full_name; next }
            unless ($entry =~ /\.txt$/i) { next }
            if (!$most_recent_time or ($most_recent_time > -M $entry_full_name)) {
                $most_recent_time = -M $entry_full_name;
                $most_recent_name = $entry_full_name;
            }
        }
    }
}

if (defined $most_recent_name) {
    print "Most recently modified: $most_recent_name"
} else {
    print "No files found."
}


哇!绝对完美!给了我想要的。格雷森·亨利,请在评论中询问你是否需要对任何作品进行解释。我花了一些时间,现在明白了。去年晚些时候开始用Perl编程,所以需要查找“next”“-M”“-d”和“除非”,但它们看起来相当直截了当。有没有关于如何让它跑得更快的建议?在一个更复杂的目录结构中,我们可能在根层有无数个文件夹,这使得搜索需要一些时间。我试图为某些文件夹添加一个case语句检查,但我当前的逻辑使它在每一层检查该文件夹(如果没有意义,请告诉我)。@GraysonHenry,如果你对目录有任何条件,请在
push@dir\u queue
之前检查它们。得到了我需要的一切,你帮我们节省了很多时间!哇!绝对完美!给了我想要的。格雷森·亨利,请在评论中询问你是否需要对任何作品进行解释。我花了一些时间,现在明白了。去年晚些时候开始用Perl编程,所以需要查找“next”“-M”“-d”和“除非”,但它们看起来相当直截了当。有没有关于如何让它跑得更快的建议?在一个更复杂的目录结构中,我们可能在根层有无数个文件夹,这使得搜索需要一些时间。我试图为某些文件夹添加一个case语句检查,但我当前的逻辑使它在每一层检查该文件夹(如果没有意义,请告诉我)。@GraysonHenry,如果你对目录有任何条件,请在
push@dir\u queue
之前检查它们。得到了我需要的一切,你帮我们节省了很多时间!哇!绝对完美!给了我想要的。格雷森·亨利,请在评论中询问你是否需要对任何作品进行解释。我花了一些时间,现在明白了。去年晚些时候开始用Perl编程,所以需要查找“next”“-M”“-d”和“除非”,但它们看起来相当直截了当。有没有关于如何让它跑得更快的建议?在一个更复杂的目录结构中,我们可能在根层有无数个文件夹,这使得搜索需要一些时间。我试图为某些文件夹添加一个case语句检查,但我当前的逻辑使它在每一层检查该文件夹(如果没有意义,请告诉我)。@GraysonHenry,如果你对目录有任何条件,请在
push@dir\u queue
之前检查它们。得到了我需要的一切,你帮我们节省了很多时间!哇!绝对完美!给了我想要的。格雷森·亨利,请在评论中询问你是否需要对任何作品进行解释。我花了一些时间,现在明白了。去年晚些时候开始用Perl编程,所以需要查找“next”“-M”“-d”和“除非”,但它们看起来相当直截了当。有没有关于如何让它跑得更快的建议?在一个更复杂的目录结构中,我们可能在根层有无数个文件夹,这使得搜索需要一些时间。我试图为某些文件夹添加一个case语句检查,但我当前的逻辑使它在每一层检查该文件夹(如果没有意义,请告诉我)。@GraysonHenry,如果你对目录有任何条件,请在
push@dir\u queue
之前检查它们。得到了我需要的一切,你帮我们节省了很多时间!