File::Path
File::Find
File::Copy
File::Basename
The modules will be help to complete the needs.
k_manimuthu
Junior Poster in Training
93 posts since Jun 2009
Reputation Points: 55
Solved Threads: 24
For example:
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use File::Path qw(make_path);
use File::Basename;
use File::Copy;
my @directories_to_search = ('/home/david/Programming/data');
my @suffixes = qw(.axf .fls .txt .err .dummy .log .cfg);
find(\&wanted, @directories_to_search);
sub wanted{
my($filename, $directories, $suffix) = fileparse($File::Find::name, @suffixes);
if ($suffix){
my $target_dir = "/home/david/test/$directories";
make_path($target_dir);
copy($File::Find::name,"$target_dir/$filename$suffix") or die "Copy failed: $!";
}
print "Copied$filename$suffix\n";
}
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
I don't see what causes that error. Taking a second look at the script I see that the print statement should be inside the if block because we only want to print info about the files with the desired extension, since they are the only files to be copied. Try putting the print statement before the copy statement so we can see what it is going to try to copy before the error occurs. The following may work, or at least print better information for debugging:
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use File::Path qw(make_path);
use File::Basename;
use File::Copy;
# Target Directory
#my @directories_to_search = ('HW/TARGET_PLATFORM/MODEM_DEBUG');
my @directories_to_search = ('/home/david/Programming/data');
#my @suffixes = qw( .axf .fls .txt .cfg .log );
my @suffixes = qw(.o);
find(\&wanted, @directories_to_search);
sub wanted{
my($filename, $directories, $suffix) = fileparse($File::Find::name, @suffixes);
if ($suffix){
#my $target_dir = "/home/mmhuqx/test/$directories";
my $target_dir = "/home/david/Programming/test/$directories";
make_path($target_dir);
print "Try to copy $File::Find::name ... Extension is $suffix\n";
copy($File::Find::name,"$target_dir/$filename$suffix") or die "Copy failed: $!";
#print only if copied.
#print "Copied $File::Find::name ... Extension is $suffix\n";
}
}
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
I found my mistake.
my @suffixes = qw( .axf .fls .txt .cfg .log .o);
is not quite right because each pattern is treated as a regex (regular expression) and the dot is a special regex character which needs to be 'escaped' by preceding it with a backslash in order to represent a dot character in the extension. Instead we should escape the dot in all the patterns, like this: my @suffixes = qw( \.axf \.fls \.txt \.cfg \.log \.o);
Does the following script do what you want?
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use File::Path qw(make_path);
use File::Basename;
use File::Copy;
# Target Directory
#my @directories_to_search = ('HW/TARGET_PLATFORM/MODEM_DEBUG');
my @directories_to_search = ('/home/david/Programming/data');
#my @suffixes = qw( \.axf \.fls \.txt \.cfg \.log );
my @suffixes = qw(\.o);#Backslash to escape regex special character dot ('.')
find(\&wanted, @directories_to_search);
sub wanted{
my($filename, $directories, $suffix) = fileparse($File::Find::name, @suffixes);
if ($suffix){
#my $target_dir = "/home/mmhuqx/test/$directories";
my $target_dir = "/home/david/Programming/test/$directories";
make_path($target_dir);
print "Try to copy $File::Find::name ... Extension is $suffix\n";
copy($File::Find::name,"$target_dir/$filename$suffix") or die "Copy failed: $!";
#print only if copied.
#print "Copied $File::Find::name ... Extension is $suffix\n";
}
}
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
I haven't tested it under Windows, only Linux. However I don't see any reason why it shouldn't work just as well in a Windows environment.
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159