简体   繁体   English

perl脚本来计数Windows目录树中的文件

[英]perl script to count files in windows directory tree

I am new to perl scripting. 我是perl脚本开发的新手。 I am trying to get the count of directories & subdirectories. 我正在尝试获取目录和子目录的数量。 So I have searched all the available help on scripting. 因此,我搜索了有关脚本的所有可用帮助。 But unable get the count of Subdirectories. 但是无法获得子目录的数量。 Below is the script I used. 以下是我使用的脚本。

use strict;
use warnings;
use File::Slurp;

my @dirs            = ('.');
my $directory_count = 0;
my $file_count      = 0;
my $outfile         = 'log.txt';

open my $fh, '>', $outfile or die "can't create logfile; $!";
for my $dir (@dirs) {
    for my $file (read_dir ($dir)) {
        if ( -d "$dir/$file" ) {
            $directory_count++;
        }
        else {
            $file_count++;
        }
    }
    print $fh "Directories: $directory_count\n";
    print $fh "Files: $file_count\n";
}
close $fh;

Here, I am unable to identify where to change the command of dir with /s. 在这里,我无法确定使用/ s更改dir命令的位置。 Please help it will reduce lot of manual work. 请帮助它会减少很多手工工作。

Ravi 拉维

Never EVER write your own directory traversal. 永远不要编写自己的目录遍历。 There are too many pitfalls, gotchas and edge cases. 陷阱,陷阱和边缘案例太多了。 Things like path delimiters, files with spaces, alternate data streams, soft links, hard links, DFS paths... just don't do it. 诸如路径定界符,带空格的文件,备用数据流,软链接,硬链接,DFS路径之类的东西只是不做。

Use File::Find or if you prefer File::Find::Rule . 使用File::Find或者如果您喜欢File::Find::Rule

As I prefer the former, I'll give an example: 我更喜欢前者,我将举一个例子:

use strict;
use warnings;

use File::Find;

my $dir_count;
my $file_count;

#find runs this for every file in it's traversal.
#$_ is 'current file'. $File::Find::Name is full path to file. 
sub count_stuff {
    if ( -d ) { $dir_count++ };
    if ( -f ) { $file_count++ };
}

find ( \&count_stuff, "." );

print "Dirs: $dir_count\n";
print "Files: $file_count\n";

Here is a script that does it: 1) without global variables; 这是执行此操作的脚本:1)没有全局变量; and 2) without adding another sub to the namespace. 和2)不向名称空间添加其他子项。

#!/usr/bin/env perl

use strict;
use warnings;
use File::Find;

run(\@ARGV);

sub run {
    my $argv = shift;
    for my $dir ( @$argv ) {
        my $ret = count_files_and_directories( $dir );
        printf(
            "%s: %d files and %d directories\n",
            $dir,
            $ret->{files},
            $ret->{directories}
        );
    }
    return;
}

sub count_files_and_directories {
    my $top = shift;
    my %ret = (directories => 0, files => 0);
    find(
        {
            wanted => sub {
                -d and $ret{directories} += 1;
                -f and $ret{files} += 1;
            },
            no_chdir => 1,
        },
        $top,
    );
    \%ret;
}

It seems simpler to use File::Find::Rule .. For example: 使用File::Find::Rule似乎更简单。例如:

use warnings;
use strict;
use File::Find::Rule;

my @files = File::Find::Rule->new->file->in('.');
my @dirs = File::Find::Rule->new->directory->in('.');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM