Hi,
I am trying to search a directory for subdirectories, recreate the subdirectories elsewhere and copy only certain files from current subdirectory to new subdirectory.
In other words i am sorting filetypes into subdirectories with same name.
so far i have been able to create the subdirectories, but i am not able to copy the files into those directories.
Can anyone please tell my how to use the glob? bcoz i am unable to copy the files.
Thanks in advance
I am trying to search a directory for subdirectories, recreate the subdirectories elsewhere and copy only certain files from current subdirectory to new subdirectory.
In other words i am sorting filetypes into subdirectories with same name.
so far i have been able to create the subdirectories, but i am not able to copy the files into those directories.
Code:
my code is :
use strict;
use warnings;
use File::Glob;
use File::Copy;
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 = 0;
sub dir_read
{
#parse directory for directories and files
#local to this function
my @dir_list;
my @file_list;
my $dir_prefix = $_[0];
my @gel_list;
my @res_list;
print "reading dir: ${dir_prefix}\n";
opendir(aDIR, $dir_prefix);
#read, add to array
while($_ = readdir(aDIR))
{
#if a dir
if(-d "${dir_prefix}/${_}")
{
#dont allow . or ..
if($_ ne "." && $_ ne "..")
{
#add to array
push(@dir_list, "${dir_prefix}/${_}");
}
}
#else a file
else
{
#filter for files
if($count ne 0) {
@gel_list = <$dir_list[$count]/*.gel>;
@res_list = <*.res>;
foreach $_ (@gel_list){
print"before file copy $_";
copy( "$_", "$dir_dest_gel\\$dir_list[$count]\\$_"); }
foreach $_ (@res_list){
print"before file copy $_";
copy("$_", "$dir_dest_res\\$dir_list[$count]\\$_"); }
}
push(@file_list, "${dir_prefix}/${_}");
}
}
closedir(aDIR);
#print dir and file list:
foreach $_ (@dir_list)
{
print "\tdir: ${_}\n";
}
foreach $_ (@file_list)
{
print "\tfile: ${_}\n";
}
#print iteration
print"$count\n";
$count = $count + 1;
#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 trial2.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";
}
Can anyone please tell my how to use the glob? bcoz i am unable to copy the files.
Thanks in advance
Comment