yaduraj said:
[...]
if i have to copy all the files with .c extension like, what is the
method to this...
....
my $source='source'; # insert the source path (without trailing /)
my $target='target'; # insert the target path
my $select='^.*\.c$'; # this is the RegExpr equivalent to "*.c"
opendir(DIR,$source);
my @strings=readdir(DIR);
closedir(DIR);
map { system("cp $source/$_ $target") } grep(/$select/,@strings);
There isn't much of a point to going through all of that if all you are
going to do is call system and not even check it's return value.
cp is not generally available on Win32 systems.
If you are going to go that route, why not just use:
system('cp', "$source/*.c", $target) == 0
or die "System failed: $?;
If there are a lot of entries in the directory, building the @strings array
can be expensive.
Here is one way to do it in a more portable way (other alternatives include
using File::Find, File::Finder or File::Find:Rule).
#! perl
use strict;
use warnings;
use File::Copy;
use File::Spec::Functions qw/catfile/;
my $source_dir = catfile 'D:', 'Home', 'Dload';
my $target_dir = catfile $source_dir, 'Jpeg';
opendir my $dir, $source_dir
or die "Cannot open directory $source_dir: $!";
while(defined (my $f = readdir $dir)) {
next unless $f =~ /.+\.jpg$/i;
my $target = catfile $target_dir, $f;
if(-e $target) {
# maybe ask the user if it is OK to overwrite?
}
my $source = catfile $source_dir, $f;
next unless -f $source;
if(copy $source, $target) {
print "$source copied to $target\n";
} else {
print STDERR "Failed to copy $source to $target\n";
}
}
closedir $dir or die "Cannot close directory: $!";
__END__
Sinan.