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

Copy function doesnt seem to work

P: 12
Hi,
i am trying to copy files of a certain type into a different directory using perl,
The copy function doesnt seem to work.
Expand|Select|Wrap|Line Numbers
  1. my @dir_list;
  2.           my $sub_dir;
  3.           my $file_name1;
  4.           my $file_name2;
  5.       my @file_list_gel;       
  6.           my @file_list_res;
  7. #--------------LINE21
  8.       opendir(aDIR, $dir_root);
  9.       @dir_list = grep !/^\.\.?$/, readdir aDIR;
  10.           closedir(aDIR); 
  11.     # if .gel or .res file is found in the current directory make a copy in respective folder
  12.         foreach $sub_dir(@dir_list)
  13.         {    
  14.         # make subdirectories in TS and results folder                 
  15.                  mkdir "$dir_dest_gel\\$sub_dir" or die $!;
  16.          mkdir "$dir_dest_res\\$sub_dir" or die $!; 
  17. #------------Line 31
  18.         print"Sub directory :$sub_dir\n";
  19.  
  20.         opendir(DIR, "$dir_root\\$sub_dir");
  21.                 while ($_ = readdir(DIR))
  22.         {
  23.             if($_ =~ /\.gel/)
  24.                         { 
  25.                 print"File name: $_\n";
  26.                 print"Dest Dir: $dir_dest_gel\\$sub_dir";
  27.                              copy("$_", "$dir_dest_gel\\$sub_dir\\$_") or die $!;
  28.                      }
  29.              if($_ =~ /\.res/)
  30.                      { 
  31.                           copy("$_", "$dir_dest_res\\$sub_dir\\$_") or die $!;
  32.                      }
  33.         }               
  34.                 closedir(DIR);
  35.      }
  36.  
it is printing the file name and destination directory correctly,
but copy function is not copying the file
Jul 8 '08 #1
Share this Question
Share on Google+
4 Replies


KevinADC
Expert 2.5K+
P: 4,059
You hopefully just need to use chomp:

Expand|Select|Wrap|Line Numbers
  1. opendir(DIR, "$dir_root\\$sub_dir");
  2. while ($_ = readdir(DIR))
  3. {
  4. chomp;
  5. if($_ =~ /\.gel/)
  6. .....
Jul 8 '08 #2

P: 12
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
Jul 10 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Perl has no copy() function. I assume you loaded the File::Copy module?
Jul 10 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Jul 11 '08 #5

Post your reply

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