简体   繁体   中英

Perl Copy to more than one Location

Hello I'm attempting to copy a file to more then one location. I have written a script that will copy files using File::Find and File::Copy together to one location but I can not get the script to copy to a second or third location. I've tried to add a second variable target2 to the script so I can also copy the JPG files to this second target location. When I try to do this I get an error message. I want the copy to run on a loop that will copy the files X amount of seconds so I added the sleep function in as well. Can anyone explain to me why I can't copy to more than one location or help me find a way to do this? Thank you.

   while (1)
   { sleep (10);
    find(
       sub {
         if (-f &&/\.jpg$/i) {
           print "$File::Find::name -> $target, $target2";
           copy($File::Find::name, $target ,$target2)
            or die(q{copy failed:} . $!);

            }
           },
           @source
             ); 

            }

Error message: Bad Buffer size for copy: 0

copy function can't copy to 2 destinations. Call it twice:

copy($File::Find::name, $target);
copy($File::Find::name, $target2);

Look here to see the parameters explanation: http://perldoc.perl.org/File/Copy.html#SYNOPSIS

As the docs say:

An optional third parameter can be used to specify the buffer size used for copying. This is the number of bytes from the first file, that will be held in memory at any given time, before being written to the second file.

So, when you specified the third parameter, Perl understood, that you want to set the buffer size manually. But you gave a string instead of a number, so it converted the string to a number: 0, and gave you an error:

Bad Buffer size for copy: 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM