473,385 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

154 100+
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
8 3315
KevinADC
4,059 Expert 2GB
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
jonathan184
154 100+
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
4,059 Expert 2GB
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
jonathan184
154 100+
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
4,059 Expert 2GB
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
@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
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
jonathan184
154 100+
Thank you guys that helped me. I really appreciate it.
Feb 14 '07 #9

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

Similar topics

8
by: RML | last post by:
hey guys, i am looking at this piece of code that lists numbers of files in a directory. i want to convert it so it lists the files in th directory that end with .doc. i cant seem to get it to...
6
by: rtilley | last post by:
Hello, When working with file and dir info recursively on Windows XP. I'm going about it like this: for root, dirs, files in os.walk(path): for f in files: ADD F to dictionary for d in...
2
by: lannsjo | last post by:
I need a program that simultaneously can copy a single file (1 GB) from my pc to multiple USB-harddrives. I have searched the internet and only found one program that does this on...
1
by: Dan | last post by:
I have an application that I want to use for copying files. My goal is to copy a files, if a file is in use or not accessible because of security reasons I want to make note of that file then...
3
by: Hartmut Dippon | last post by:
Hi all, I hope somebody can help me with following problem: I have an application where I can drag&drop files/dirs from within explorer onto my form. If multiple files/dirs are selected I...
13
by: Puja | last post by:
hi all, how can I copy directory c:\test to a directory c:\backup with all files, folders and subfolders that are contained within test. please advice! thanks
4
by: GS | last post by:
from the online help, looks like to code step by step to copy content of directory to another whereas I can use online line code for directory move or delete. I am using 2005 express .net 2 I did...
5
by: alivip | last post by:
How can I get every Token (word) and PreviousToken(Previous word) From multube files and frequency of each two word my code is trying to get all single word and double word (every Token (word) and...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.