Question : Need to iterate through all the files in a dir in perl and copy them elsewhere

Hi,

I am reading through an excel sheet, selecting out a unix location, going there and then trying to copy all .txt files from there into another directory one level up.

I am getting this error. Pls help.

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

Use of uninitialized value in concatenation (.) or string at Copy_Baseline.pl at the glob statement line.
/ms/user/s/sayantag/Baseline/
-------------------------------------------


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
#!/usr/bin/perl5.10 -w


use warnings;
use Spreadsheet::WriteExcel::Big;
use Spreadsheet::ParseExcel;
use strict;
use IO::Scalar;
use Data::Dumper;

my $CFP = "/ms/user/s/sayantag/";
my $new_parser   = Spreadsheet::ParseExcel->new();
my $new_workbook = $new_parser->parse($CFP . "Source.xls");
my $new_worksheet;

my $curr_etl = "wf_OTC_Taxlots_Open";


for  $new_worksheet ( $new_workbook->worksheets() ) {
next unless $new_worksheet->{Name} eq "WF";
#print "xyb";
my ( $row_min, $row_max ) = $new_worksheet->row_range();
my ( $col_min, $col_max ) = $new_worksheet->col_range();

 for my $row ( $row_min .. $row_max ) {##First for##
 for my $col ( $col_min .. $col_max ) {##Second for##
   my $cell = $new_worksheet->get_cell( $row, $col );
     if ($col==0 && $row !=0 && ($new_worksheet->get_cell($row, $col)->value eq "$curr_etl"))  {
       my $wf_loc = $new_worksheet->get_cell($row, $col+2)->value;

       chdir($wf_loc) or die "$!";
       print "$wf_loc";

       my @delta_files = glob "$ARGV[0]*.txt";

       foreach my $txt_file (@delta_files) {
         `cp $txt_file ../Delta_ETL/`;
           }
         }
       }
     }
   }

Answer : Need to iterate through all the files in a dir in perl and copy them elsewhere

Changed to match your updates you posted while I was writing that...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
#!/usr/bin/perl5.10

# -w is redundant with use warnings
use warnings;
use Spreadsheet::WriteExcel::Big;
use Spreadsheet::ParseExcel;
use strict;
use IO::Scalar;
use Data::Dumper;
use File::Copy qw(cp);

my $CFP = "/ms/user/s/sayantag/";
my $new_parser   = Spreadsheet::ParseExcel->new();
my $new_workbook = $new_parser->parse($CFP . "Source.xls");

my $curr_etl = "wf_OTC_Taxlots_Open";
my $wf_name = substr $curr_etl, 3;
print "The workflow name is $wf_name\n";

for  my $new_worksheet ( $new_workbook->worksheets() ) {
    next unless $new_worksheet->{Name} eq "WF";
    #print "xyb";
    my ( $row_min, $row_max ) = $new_worksheet->row_range();
    my ( $col_min, $col_max ) = $new_worksheet->col_range();

    for my $row ( $row_min .. $row_max ) {##First for##
        my $cell = $new_worksheet->get_cell($row, 0);
        if (defined $cell and $cell->value eq $curr_etl)  {
            my $wf_loc = $new_worksheet->get_cell($row, 2)->value;

            print "$wf_loc";

            opendir DIR, $wf_loc
                or die "could not open $wf_loc: $!";
            my @delta_files = grep m{^delta_${wf_name}_Baseline\.txt$}, readdir DIR;
            closedir DIR;

            foreach my $txt_file (@delta_files) {
                cp "$wf_loc/$txt_file" "$wf_loc/../Delta_ETL/$txt_file"
                    or die "could not cp $wf_loc/$txt_file: $!";
            }
        }
    }
}
Random Solutions  
 
programming4us programming4us