| re: copying files of a certain type to different folder
Hi,
i modified my code, now i am able to print the path where i want the file to go, but still the file is not getting copied.
Please tell me what is wrong in the code below.
use strict;
use warnings;
use File::Copy;
use Cwd;
my $dir_root; #dir to start in
my $dir_dest_gel = 'C:\TS'; #destination directory for gel files
my $dir_dest_res = 'C:\results'; # destination directory for results
#my $count;
sub dir_read
{
#parse directory for directories and files
#local to this function
my @Dir_name;
my @dir_list;
my $dir_prefix = $_[0];
print "reading dir: ${dir_prefix}\n";
opendir(aDIR, $dir_prefix);
#read, add to array -----line no 25
while($_ = readdir(aDIR))
{
#if a dir
if(-d "${dir_prefix}/${_}")
{
#dont allow . or ..
if($_ ne "." && $_ ne "..")
{
#add to array36
push(@dir_list, "${dir_prefix}/${_}"); }
}
#else a file--------line no 37
else
{
@Dir_name = split m!/!,$dir_prefix;
if($_ =~ /\.gel/)
{
copy("$_", "$dir_dest_gel\\$Dir_name[-1]\\$_");
}
if($_ =~ /\.res/)
{
copy("$_", "$dir_dest_res\\$Dir_name[-1]\\$_");
}
}
}
closedir(aDIR);
#print dir and file list:74
foreach $_ (@dir_list)
{
print "\tdir: ${_}\n";
}
#search lower dirs
foreach $_ (@dir_list)
{
# make subdirectories in TS and results folder
mkdir "$dir_dest_gel\\${_}" or die $!;
mkdir "$dir_dest_res\\${_}" or die $!;
&dir_read($_);
}
}
if(! $ARGV[0])
{
#print usage
print "Usage: perl trial3.pl rootpath\n";
}
else
{
# create main destination directories
mkdir $dir_dest_gel;
mkdir $dir_dest_res;
#check if path exists
$dir_root = $ARGV[0];
if( -d $dir_root)
{
#dir is ok
print "root directory: ${dir_root}\n";
}
else
{
#end
die("root directory: ${dir_root} does not exist!, stopped\n");
}
#read dir, and sub directories
&dir_read($dir_root);
print "done.\n";
}
|