Copying sub directories

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

    Copying sub directories

    Hi,
    I would like to know how to copy certain types of files from a group of directories into another group of directories.
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    What have you tried? In the meantime look into mkdir, readir, File Find and File::Copy to get you started.

    --Kevin

    Comment

    • supriyamk
      New Member
      • Jul 2008
      • 12

      #3
      Hi,
      i am trying to copy 1 set of files to a folder from an existing directory tree.
      Can you please tell me what is wrong with this code?

      Code:
      use strict;
      use warnings;
      use File::Copy;
      use File::Glob;
      
      my $Src_Dir = 'C:\Documents and Settings\abcd';
      my $Dest_Dir_Gel = 'C:\gel';
      my $Dest_Dir_Res = 'C:\Results';
      
      Sub dir_read
      {
       # parse directory and sub directories for files
      
       # Declarations
       my @Dir_List;
       my @Gel_List;
       my @Res_List;
       my $Dir_Name = $_[0];
      
       # Create directories with module name 
       mkdir "$Dest_Dir_Gel\$Dir_Name";
       mkdir "$Dest_Dir_Res\$Dir_Name";
      
       foreach (@Dir_List){
      
        opendir(cDIR, $Dir_Name);
       
        # List the .gel and .res files 
        @Gel_List = <*.gel>;
        @Res_List = <*.res>;
       
        #copy files to folder
        foreach (@Gel_List){
          copy($_,"$Dest_Dir_Gel\$Dir_Name");
        }
        
        #copy  files to  folder
        foreach (@Res_List){
          copy($_,"$Dest_Dir_Res\$Dir_Name");
        }
      
       closedir(cDIR);
       }
      }
      
      &dir_read($Src_Dir);
      Last edited by eWish; Jul 2 '08, 10:45 PM. Reason: Please use code tags

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        The variable $Dir_Name is causing the problem. You are passing $Src_Dir as argument to dir_read subroutine. Hence, the line,
        Code:
         my $Dir_Name = $_[0];
        will assign 'C:\Documents and Settings\abcd' to $Dir_Name. So, the line
        Code:
        mkdir "$Dest_Dir_Gel\$Dir_Name";
        will be like mkdir "C:\gel\C:\Docu ments and Settings\abcd" :(

        process the argument for read_dir() to get only the desired string as sub-folder structure.

        Comment

        Working...