0

I want to copy the text file from all folders and sub-folders and keep it in different locations. I write the code, but it doesn't work. It is not able to recognize sub-folders. It reads the text, but not recognize is it a file or folder.

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";
}
Toto
  • 89,455
  • 62
  • 89
  • 125
user230391
  • 53
  • 7
  • In order to help you, you must describe in **exact** details what is not working. What have you tested? Does it fail to copy any files? – hlovdal Mar 25 '14 at 07:56
  • no, it is not enter in sub-directory. I provide you a directory structure which is more helpful to you. – user230391 Mar 25 '14 at 08:02
  • m not permitted to enter image .... – user230391 Mar 25 '14 at 08:09
  • This is not detailed enough. Is `m not permitted to enter image` an exact error message you get from running your perl script (which is called `m`?) or is it just a sentence trying to describe the problem. Please update the question with commands you run and output you get. – hlovdal Mar 25 '14 at 08:13
  • There is no error , but it is not able to read file of sub-folders – user230391 Mar 25 '14 at 08:22
  • How about just using `File::Copy::Recursive`? – Biffen Mar 25 '14 at 10:09

0 Answers0