By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,809 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,809 IT Pros & Developers. It's quick & easy.

copying files of a certain type to different folder

P: 12
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.
Expand|Select|Wrap|Line Numbers
  1. my code is : 
  2. use strict;
  3. use warnings;
  4. use File::Glob;
  5. use File::Copy;
  6.  
  7. my $dir_root; #dir to start in
  8. my $dir_dest_gel = 'C:\TS'; #destination directory for gel files
  9. my $dir_dest_res = 'C:\results'; # destination directory for results
  10. my $count = 0;
  11. sub dir_read 
  12. {
  13.       #parse directory for directories and files
  14.  
  15.       #local to this function
  16.       my @dir_list;
  17.       my @file_list;
  18.       my $dir_prefix = $_[0];  
  19.         my @gel_list;
  20.       my @res_list;
  21.  
  22.       print "reading dir: ${dir_prefix}\n";
  23.  
  24.       opendir(aDIR, $dir_prefix);
  25.  
  26.       #read, add to array
  27.       while($_ = readdir(aDIR))
  28.       {
  29.             #if a dir
  30.             if(-d "${dir_prefix}/${_}")
  31.                 {
  32.                   #dont allow . or ..
  33.                   if($_ ne "." && $_ ne "..")
  34.                       {
  35.                         #add to array
  36.                         push(@dir_list, "${dir_prefix}/${_}");
  37.  
  38.                         }
  39.                  }
  40.             #else a file
  41.             else
  42.             {
  43.               #filter for files 
  44.                 if($count ne 0) {
  45.  
  46.                 @gel_list = <$dir_list[$count]/*.gel>;
  47.         @res_list = <*.res>;
  48.                 foreach $_ (@gel_list){
  49.                 print"before file copy $_";
  50.                 copy( "$_", "$dir_dest_gel\\$dir_list[$count]\\$_"); }
  51.         foreach $_ (@res_list){
  52.                 print"before file copy $_";
  53.                 copy("$_", "$dir_dest_res\\$dir_list[$count]\\$_"); }
  54.         }
  55.               push(@file_list, "${dir_prefix}/${_}");
  56.  
  57.             }
  58.       }
  59.  
  60.       closedir(aDIR);
  61.  
  62.  
  63.  
  64.        #print dir and file list:
  65.         foreach $_ (@dir_list)
  66.             {
  67.  
  68.               print "\tdir: ${_}\n";
  69.             }  
  70.         foreach $_ (@file_list)
  71.             {
  72.               print "\tfile: ${_}\n";
  73.             }  
  74.  
  75. #print iteration
  76. print"$count\n";
  77. $count = $count + 1;
  78.  
  79.      #search lower dirs
  80.       foreach $_ (@dir_list)
  81.           {
  82.          # make subdirectories in TS and results folder                 
  83.                  mkdir "$dir_dest_gel\\${_}" or die $!;
  84.          mkdir "$dir_dest_res\\${_}" or die $!;                 
  85.             &dir_read($_);
  86.           }
  87. }
  88.  
  89.  
  90. if(! $ARGV[0])
  91.     {
  92.       #print usage
  93.       print "Usage: perl trial2.pl rootpath\n";  
  94.     }
  95. else 
  96.     {
  97.          # create main destination directories
  98.          mkdir $dir_dest_gel;
  99.          mkdir $dir_dest_res;
  100.  
  101.       #check if path exists
  102.       $dir_root = $ARGV[0];
  103.       if( -d $dir_root)
  104.           {
  105.             #dir is ok
  106.             print "root directory: ${dir_root}\n";
  107.           }
  108.       else
  109.           {
  110.             #end
  111.             die("root directory: ${dir_root} does not exist!, stopped\n");
  112.           }
  113.  
  114.       #read dir, and sub directories
  115.       &dir_read($dir_root);
  116.  
  117.       print "done.\n";
  118.     }
  119.  
  120.  

Can anyone please tell my how to use the glob? bcoz i am unable to copy the files.
Thanks in advance
Jul 3 '08 #1
Share this Question
Share on Google+
4 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Have you read the CPAN page for File::Glob ? All globbing really is, is the ability to use wild cards to match many things. The module....well.....modulizes that.

Regards,

Jeff
Jul 3 '08 #2

P: 12
i am trying to group all files of a certain type into an array using glob.
but i guess that is not happening
Jul 3 '08 #3

P: 12
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";
}
Jul 3 '08 #4

P: 12
Hi thanks for the guidance.
i got to know the problem.
i had to specify the path for the source file.
as i was using read directory, i assumed the active directory would change but it was not so, so once i gave the full path for the src and dest files, the whole program worked.
thanks again
Jul 3 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.