Copy function doesnt seem to work

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

    Copy function doesnt seem to work

    Hi,
    i am trying to copy files of a certain type into a different directory using perl,
    The copy function doesnt seem to work.
    Code:
    my @dir_list;
              my $sub_dir;
              my $file_name1;
              my $file_name2;
    	  my @file_list_gel;  	 
              my @file_list_res;
    #--------------LINE21
    	  opendir(aDIR, $dir_root);
    	  @dir_list = grep !/^\.\.?$/, readdir aDIR;
              closedir(aDIR); 
    	# if .gel or .res file is found in the current directory make a copy in respective folder
            foreach $sub_dir(@dir_list)
            {	
    		# make subdirectories in TS and results folder                 
                     mkdir "$dir_dest_gel\\$sub_dir" or die $!;
    		 mkdir "$dir_dest_res\\$sub_dir" or die $!; 
    #------------Line 31
    		print"Sub directory :$sub_dir\n";
    
    		opendir(DIR, "$dir_root\\$sub_dir");
                    while ($_ = readdir(DIR))
    		{
    			if($_ =~ /\.gel/)
                            { 
    				print"File name: $_\n";
    				print"Dest Dir: $dir_dest_gel\\$sub_dir";
                         		copy("$_", "$dir_dest_gel\\$sub_dir\\$_") or die $!;
                     	}
    		 	if($_ =~ /\.res/)
                     	{ 
                     		 copy("$_", "$dir_dest_res\\$sub_dir\\$_") or die $!;
                     	}
    		}               
                    closedir(DIR);
     	}
    it is printing the file name and destination directory correctly,
    but copy function is not copying the file
    Last edited by numberwhun; Jul 9 '08, 01:52 AM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    You hopefully just need to use chomp:

    Code:
    opendir(DIR, "$dir_root\\$sub_dir");
    while ($_ = readdir(DIR))
    {
    chomp;
    if($_ =~ /\.gel/)
    { 
    .....

    Comment

    • supriyamk
      New Member
      • Jul 2008
      • 12

      #3
      i just changed the code from copy to system(copy) and it works, so i guess i am not using perl's copy function correctly

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Perl has no copy() function. I assume you loaded the File::Copy module?

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Originally posted by supriyamk
          i just changed the code from copy to system(copy) and it works, so i guess i am not using perl's copy function correctly
          For your reference, here is a list of Perl's built-in functions. You will find that, as Kevin said, there is no copy function. If you installed and used a module, then you have to use its functions in its prescribed manner, per its CPAN documentation page.

          Regards,

          Jeff

          Comment

          Working...