分别从每个子目录中提取文件

时间:2013-12-20 20:52:18

标签: perl command-line perl-module

我有两个目录,里面有很多子目录和文件 像

originalfolder > file1
                 file2
                 folder2 > file3
                           file4
                 folder3 > file1

updatedfolder > file1
                file2
                folder2 > file3
                          file4
                folder3 > file1

我必须将originalfolder的特定子目录中的所有文件与updatedfolder中的相同子目录进行比较。

这意味着我必须从一个子目录中取出所有文件,然后转到下一个子目录

但我的代码会提取文件夹中的所有文件。

CODE

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

my $path = "C:/a";
my @files = File::Find::Rule->file()
                        ->name( '*.*' )
                        ->in( $path); 

my $path1 = "C:/b";
my @files1 = File::Find::Rule->file()
                        ->name( '*.*' )
                        ->in( $path1);

我可以做比较文件大小部分。

我只需要帮助一次遍历一个子目录并将文件解压缩到数组中。

窗口命令也会这样做..因为我可以在必要时在Perl中运行窗口命令

2 个答案:

答案 0 :(得分:1)

这将返回“C:/ a”下的目录。条件if -d $ File :: Find :: name在将其推入@files数组之前检查它是一个目录还是文件。

use File::Find qw(finddepth);

my $path = "C:/a";
my @files;
finddepth(sub {
  return if($_ eq '.' || $_ eq '..');
  push @files, $File::Find::name if -d $File::Find::name;
}, $path);

答案 1 :(得分:1)

您可以限制子目录的级别。尝试将其限制为1以仅搜索特定文件夹:

my @files = File::Find::Rule->file()
                    ->name( '*.*' )
                    ->maxdepth(1)
                    ->in( $path);

修改

一个代码段,可以比较两个文件夹及其子文件夹,而无需手动循环遍历所有子文件夹。

尝试

perl compare.pl folder_1 folder_2

compare.pl 保存为以下内容...

#!/usr/bin/perl

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

my $first_directory_info    = { directory_name => $ARGV[0] };
my $second_directory_info   = { directory_name => $ARGV[1] };

get_directory_info( $first_directory_info );
get_directory_info( $second_directory_info );

if(
    something_is_wrong( $first_directory_info, $second_directory_info, 1 ) ||
    something_is_wrong( $second_directory_info, $first_directory_info )
)
{
    print "Something is wrong...\n";
}
else
{
    print "Everything is fine...\n";
}

sub get_directory_info
{
    my $directory_info = shift;
    my $directory_name = $directory_info->{ directory_name };

    foreach my $file ( map { $_ =~ /^$directory_name(.*)/; $1 } File::Find::Rule->file->in( $directory_name ) )
    {
        $directory_info->{ files }->{ $file }->{ file_size } = -s $directory_name.$file;
        $directory_info->{ files }->{ $file }->{ writable }  = -w $directory_name.$file;
    }
}

sub something_is_wrong
{
    my $first_directory_info    = shift;
    my $second_directory_info   = shift;
    my $check_attributes        = shift;

    my $result                  = 0; # we assume, everything is fine

    foreach my $key ( keys( $first_directory_info->{ files } ) )
    {
        unless( $second_directory_info->{ files }->{ $key } )
        {
            print "$key is missing in ".$second_directory_info->{ directory_name }."\n";
            $result = 1; # some is wrong
            next;
        }

        if( $check_attributes )
        {
            if(  $first_directory_info->{ files }->{ $key }->{ file_size } !=
                $second_directory_info->{ files }->{ $key }->{ file_size } )
            {
                print "$key has different file size in ".$second_directory_info->{ directory_name }."\n";
                $result = 1;
            }

            if(  $first_directory_info->{ files }->{ $key }->{ writable } !=
                $second_directory_info->{ files }->{ $key }->{ writable } )
            {
                print "$key has different permissions in ".$second_directory_info->{ directory_name }."\n";
                $result = 1;
            }
        }
    }

    return $result;
}

exit;