从perl中的文件夹和子文件夹中复制所有文件?

时间:2014-03-25 07:41:52

标签: regex perl-module perl-io

我想从所有文件夹和子文件夹中复制文本文件,并将其保存在不同的位置。 我编写代码,但它不起作用。它无法识别子文件夹。它读取文本,但无法识别它是文件还是文件夹。

CODE

use File::Copy;
use File::Find::Rule;
use warnings;

my $PATH1 = "/workspaces/PerlWorkspace/TestProj";
my $PATH2 = "/workspaces/PerlWorkspace/TestProj/Test/";
opendir( my $DIR, $PATH1 ) || die "can't opendir  : $!";
my @files = readdir($DIR);

foreach my $t (@files) {
print $t. "\n";
if ( $t =~ m/.txt/ ) {
    #Check txt file only for files (no directories)
    copy "$PATH1/$t", "$PATH2/$t";
}
next if $t eq ".";
next if $t eq "..";
next if $t eq ".project";
if ( ( -d $t ) ) {
    if ( !( $t eq "Test" ) ) {
        my $PATHSUB = "$PATH1/$t";
        print $PATHSUB. "\n";
        Sub_Dir( $t, $PATHSUB );
    }
}
}
closedir($DIR);

sub Sub_Dir {
my $Dir = shift;
print $Dir. "\n";
$PATHSUB1 = shift;
print $PATHSUB1. "\n";

opendir( my $DIRSUB, $PATHSUB1 ) || die "can't opendir: $!";
    my @Subfiles = readdir($DIRSUB);
foreach my $t1 (@Subfiles) {
    print $t1. "\n";
    if ( $t1 =~ m/.txt/ ) {
        #Check txt file only for files (no directories)
        copy "$PATHSUB1/$t1", "$PATH2/$t1";
    }
    next if $t1 eq ".";
    next if $t1 eq "..";
    if ( ( -d $t1 ) ) {
        if ( !( $t1 eq "Test" ) ) {
            $PATHSUB1 = "$PATHSUB1/$t1";
            print $PATHSUB1. "\n";
            Sub_Dir( $t1, $PATHSUB1 );
        }
    }
}
my @a = split( '/', $PATHSUB1 );
my $size = @a;
print "size:" . $size;
for ( my $i = $size - 1 ; $i > $size - 2 ; $i-- ) {
    pop(@a);
}
$PATHSUB1 = join( '/', @a );
print "path::$PATHSUB1\n";
}

0 个答案:

没有答案