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

trying to copy files to multiple dirs where destinations are on txt

100+
P: 154
Hi I am trying to copyfiles and dirs keeping there permissions intact and copy to multiple destinations which are on a data.txt file
on the txt file i put
test1
test2

So where ami going wrong?

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl
  3. use File::Copy;
  4.  
  5. my $srcdir = "/var/www/virtual/website/htdocs/drop";
  6.  
  7. opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
  8. @files = readdir(DIR);
  9. close(DIR);
  10.  
  11. open (MYFILE, '/var/www/virtual/website/htdocs/data.txt');
  12. foreach (<MYFILE>) {
  13.     chomp;
  14.     print "$_\n";
  15.     my $file = @files;
  16.     my $dest = "/var/www/virtual/website/htdocs/$_";
  17.     close (MYFILE);
  18.  
  19.     my $file = @files;
  20.     my $source = "$srcdir/$file";
  21.  
  22.     copy($source, $dest) or die "Copy $old -> $dest failed: $!";
  23.     print "copy completed";
  24. }
  25.  
  26.  
  27.  
Feb 13 '07 #1
Share this Question
Share on Google+
8 Replies


KevinADC
Expert 2.5K+
P: 4,059
I'm a bit confused.

@files has a lits of files and folders? Do you want to copy all those files and folders to new folder(s) : test1 and test2?
Feb 13 '07 #2

100+
P: 154
yes that is correct, the destination folder names would be in the text file


I'm a bit confused.

@files has a lits of files and folders? Do you want to copy all those files and folders to new folder(s) : test1 and test2?
Feb 13 '07 #3

KevinADC
Expert 2.5K+
P: 4,059
OK, well, unfortunately your code is not correct, as you already know since it's not doing what you hoped. I have to run right now, but I'll check back later and see if you still need help.
Feb 13 '07 #4

100+
P: 154
I am getting this error when doing this code
Unsuccessful stat on filename containing newline at D:/Perl/lib/File/Copy.pm lin
e 95.
Copy d:\prod\test\drop -> d:\prod\test\test1
failed: No such file or directory at copynow.pl line 22.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. #use warnings;
  4. #use strict;
  5.  
  6. use File::Copy;
  7.  
  8. open (MYFILE, 'd:\\prod\\test\data.txt');
  9. for $line (<MYFILE>) {
  10.     chomp;
  11.     print "This is the destination $line\n";
  12.     close (MYFILE);
  13.  
  14.     my $srcdir = "d:\\prod\\test\\drop";
  15.     my $dest = "d:\\prod\\test\\$line";
  16.  
  17.  
  18.     copy($srcdir, $dest) or die "Copy $srcdir -> $dest failed: $!";
  19.  
  20.     print "\n\n****** script restarting *******\n";
  21.     sleep 2;
  22. }
  23.  

OK, well, unfortunately your code is not correct, as you already know since it's not doing what you hoped. I have to run right now, but I'll check back later and see if you still need help.
Feb 13 '07 #5

KevinADC
Expert 2.5K+
P: 4,059
I think your last bit of code should be like this:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. #use warnings;
  4. #use strict;
  5.  
  6. use File::Copy;
  7.  
  8. open (MYFILE, 'd:/prod/test/data.txt') or die "$!";
  9. while (<MYFILE>) {
  10.    chomp;
  11.    print "This is the destination $_\n";
  12.    my $srcdir = "d:/prod/test/drop";
  13.    my $dest = "d:/prod/test/$_";
  14.    copy($srcdir, $dest) or die "Copy $srcdir -> $dest failed: $!";
  15.    print "\n\n****** script restarting *******\n";
  16.    sleep 2;
  17. }
  18. close(MYFILE);
  19.  
but I am not sure it will work even with the code corrected. Note that you can use forward-slashes in the file paths even though you are probably using windows.
Feb 14 '07 #6

docsnyder
P: 88
@jonathan184

File::Copy() does not work recursively.

Try this:
Expand|Select|Wrap|Line Numbers
  1. use File::Copy;
  2.  
  3. $destDirFile = '<file that contains the target directories>';
  4. $destRoot    = '<destination root dir>';
  5. $srcDir      = '<src dir>';
  6.  
  7. sub fCopy {
  8.   my($src, $dest) = @_;
  9.   my($user, $group, $mode) = (stat($src))[4,5,2];
  10.   my($srcDir, $destDir);
  11.  
  12.   ($destDir = $dest) =~ s!^(.*)/[^/]+$!$1!;
  13.   ($srcDir  = $src)  =~ s!^(.*)/[^/]+$!$1!;
  14.  
  15.   mkdir($destDir, (stat($srcDir)[2])) if ( ! -d $destDir );
  16.  
  17.   copy($src, $dest);
  18.   chmod($mode, $dest);
  19.   chown($user, $group);
  20. }
  21.  
  22. sub rCopy {
  23.   my($root, $src, $dest) = @_;
  24.   my($srcPath)  = $root . ($src ? '/' : '') . $src;
  25.   my($destPath) = $dest . ($src ? '/' : '') . $src;
  26.   my($fd);
  27.  
  28.   if ( -d $srcPath ) {
  29.  
  30.     opendir($fd, $srcPath) or die "$!";
  31.  
  32.     for $file ( readdir($fd) ) {
  33.       next if ( $file =~ /\.\.?/ );
  34.       rCopy($root, $src . ($src ? '/' : '') . "$file", "$dest");
  35.     }
  36.  
  37.     closedir($fd);
  38.   }
  39.   elsif ( -f $srcPath ) {
  40.     fCopy($srcPath, $destPath);
  41.   }
  42. }
  43.  
  44. open (MYFILE, $destDirFile) or die "$!";
  45.  
  46. while (<MYFILE>) {
  47.    chomp;
  48.  
  49.    print "This is the destination $_\n";
  50.  
  51.    my($dest) = "$destRoot/$_";
  52.  
  53.    rCopy($srcDir, undef, $dest);
  54. }
  55.  
  56. close(MYFILE);
Greetz, Doc
Feb 14 '07 #7

docsnyder
P: 88
Oops,
Expand|Select|Wrap|Line Numbers
  1. chown($user, $group);
should be
Expand|Select|Wrap|Line Numbers
  1. chown($user, $group, $src);
;o)
Feb 14 '07 #8

100+
P: 154
Thank you guys that helped me. I really appreciate it.
Feb 14 '07 #9

Post your reply

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