copying files of a certain type to different folder

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • supriyamk
    New Member
    • Jul 2008
    • 12

    copying files of a certain type to different folder

    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.
    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
    Last edited by numberwhun; Jul 3 '08, 06:56 PM. Reason: Please use code tags
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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

    Comment

    • supriyamk
      New Member
      • Jul 2008
      • 12

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

      Comment

      • supriyamk
        New Member
        • Jul 2008
        • 12

        #4
        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_pref ix}/${_}")
        {
        #dont allow . or ..
        if($_ ne "." && $_ ne "..")
        {
        #add to array36
        push(@dir_list, "${dir_pref ix}/${_}"); }
        }
        #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";
        }

        Comment

        • supriyamk
          New Member
          • Jul 2008
          • 12

          #5
          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

          Comment

          Working...