Remark: Posted this in reply to an unformated dupe (Reason for the code tags comment)
Hi,
You should put your code inside <code> tags - it makes the code more readable. If not one has to look up the source of the webpage to get a readable format. You might consider reading
Writeup Formatting Tips.
Trying to make sense of your code (put inside <code> tags):
#!/usr/bin/perl
use File::Copy;
$dest="/home/dpavu2/users1/perl/sra/";
print "the source direcotry name:\n";
chomp(my $source = <STDIN>);
opendir(DIR, $sech $file(@files){
@files = grep { /\.log$/ } readdir (DIR);
foreach $file(@files){
}
closedir (DIR);
i guess you were trying to do the following:
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy;
use File::Spec;
my $dest="/home/dpavu2/users1/perl/sra/";
print "the source direcotry name: \n";
my $source = '';
chomp($source = <STDIN>);
if (opendir(my $DIR, $source)){
my @files = grep { /\.log$/ } readdir ($DIR);
foreach my $f (@files){
my $src = File::Spec->catfile($source , $f);
my $dst = File::Spec->catfile($dest , $f);
copy($src , $dst) or die "Failed to copy $src to $dest ($!)\n"
+;
}
closedir ($DIR);
} else {
warn "Failed to open '$source ' for reading ($!)\n";
}
The Module
File::Copy::Recursive is also quite practical when copying files. You should also always include
use strict;
use warnings;
directives at the beginning of your perl code. If you don't understand an error message then
use diagnostics;
might create error messages which make more sense to you.
Cheers
Roland