如何打印计数文件的名称?

时间:2013-04-18 17:26:31

标签: perl

我是PERL的新手,已经获得了我的第一个有用的脚本!现在我想增强它,但不明白如何使用数组来实现我的目标。我看过很多文章和帖子,但还不明白。

我工作的脚本计算给定扩展名的给定目录中的文件数并输出数字。我还想做的是将文件名打印到初始指定目录中的.txt文件。

感谢任何建议或意见!我确信我需要使用数组来实现这个目标,我只是不明白如何将计数的文件名输入其中。我能够打印出数组列表,我只需要一些帮助填充数组!非常感谢!

处于当前状态的脚本:

#!usr/bin/perl
use strict;
use warnings;
use diagnostics;
use File::Find;

print "\n\n";
print "This script will start at the given directory and\nrecursively count the files of a given type\n\n\n";
print "-----------------------------------------------------------\n\n\n";
print "What directory would you like to start the count?\n\nDirectory Path: ";
my $dir = <STDIN>; #directory to begin search
chomp $dir;

print "\nWhat is the file extension you are searching for?\n\nFile Extension(.htm, .plx, .txt, etc.): ";
my $filext = <STDIN>; #file extension we're searching for
chomp $filext;

my $count = 0;


find(sub{$count++ if $File::Find::name =~ /$filext$/}, $dir);



     if ($count > 0){
     print "\n$count files counted, \n"; #display the number of files counted with the given file extension in the given directory

     }

     else {
     print "Couldn't find any files to count.\n"; #if no files of the given type are found in the given directory
     }

更新:

谢谢Wes。我现在看看它是如何运作的,并感谢您抽出时间作出回应。

对于任何有兴趣的人,这是最终的代码:

#!usr/bin/perl
use strict;
use warnings;
use diagnostics;
use File::Find;

print "\n\n";
print "This script will start at the given directory and\nrecursively count the files of a given type\n\n\n";
print "-----------------------------------------------------------\n\n\n";
print "What directory would you like to start the count?\n\nDirectory Path: ";
my $dir = <STDIN>; #directory to begin search
chomp $dir;

print "\nWhat is the file extension you are searching for?\n\nFile Extension(.htm, .plx, .txt, etc.): ";
my $filext = <STDIN>; #file extension we're searching for
chomp $filext;

my $count = 0;
my @files;
find(sub{
    if ($File::Find::name =~ /$filext$/) {
        push @files, $File::Find::name;
        $count++;
    }
}, $dir);



     if ($count > 0){
     print "\n\n-----------------------------------------------------------\n\n\n";
     print "\n$count files counted: \n\n"; #display the number of files counted with the given file extension in the given directory
     foreach (@files){
         print "$_\n";
     }


     }

     else {
     print "Couldn't find any files to count.\n"; #if no files of the given type are found in the given directory
     }

2 个答案:

答案 0 :(得分:2)

您传递给find的匿名子句可以执行多个语句,就像普通子语句一样。

my @files;
find(sub{
    if ($File::Find::name =~ /$filext$/) {
        push @files, $File::Find::name;
        $count++;
    }
}, $dir);

您也可以像这样检查数组的大小:my $count = scalar @files;而不是保持单独的计数

答案 1 :(得分:1)

这个程序会做你想要的。不需要数组,因为您可以将文件名打印到列表文件中,因为find遇到了它们。

主要区别是:

  • 在调用find之前打开输出文件以写入文件名列表

  • 将每个文件名打印到find找到的新文件

use strict;
use warnings;

use File::Find;

print "

This script will start at the given directory and
recursively count the files of a given type


-----------------------------------------------------------


What directory would you like to start the count?

Directory Path: ";

my $dir = <STDIN>;
chomp $dir;

print "
What is the file extension you are searching for?

File Extension(.htm, .plx, .txt, etc.): ";

my $filext = <STDIN>;
chomp $filext;

STDOUT->autoflush;

open my $out, '>', "$dir/file_list.txt" or die $!;
my $count = 0;
find(sub {
    return unless /\Q$filext\E$/;
    print $out "$_\n";
    $count++;
}, $dir);

if ($count > 0) {
    print "\n$count files counted\n";
}
else {
    print "Couldn't find any files to count\n";
}