I'm writing a Perl script which has its 1st step as to copy files from one directory to another directory. The Source directory has got files with extension, without extension, directories etc. But I want to copy ONLY files with no extension. The files with extensions and directories should not get copied to destination directory.
The piece of code i wrote is as below -
opendir(DIR, $source_dir) or die;
@allfiles = grep {$_ ne '.' and $_ ne '..'} readdir(DIR) or die;
@list_files = grep { !-d } @allfiles;
@dirs = grep { -d } @allfiles ;
closedir(DIR);
But the @list_files array still gets the directories into it. And @dirs array is empty. Here, i've not tried to filter the files with extensions which i wish to do in my code.
#!/usr/bin/perl -w
use warnings;
my $source_dir="/home/EDISON/test";
opendir(DIR, $source_dir) or die;
my @allfiles = grep {$_ ne '.' and $_ ne '..' } readdir(DIR) or die;
my $tmpfile;
my @list_files;
my @dirs;
for $tmpfile(@allfiles)
{
if(!-d $source_dir."/".$tmpfile)
{
push @list_files,$tmpfile;
}else
{
push @dirs,$tmpfile;
}
}
print @list_files;
print "\n";
print @dirs;
closedir(DIR);
If filter the files with extensions
#!/usr/bin/perl -w
use warnings;
my $source_dir="/home/EDISON/test";
opendir(DIR, $source_dir) or die;
my @allfiles = grep {$_ ne '.' and $_ ne '..' } readdir(DIR) or die;
my $tmpfile;
my @list_files;
my @dirs;
for $tmpfile(@allfiles)
{
if(!-d $source_dir."/".$tmpfile && $tmpfile !~ m/\.[^\.]+$/)
{
push @list_files,$tmpfile;
}else
{
push @dirs,$tmpfile;
}
}
print @list_files;
print "\n";
print @dirs;
closedir(DIR);