Assuming you can install File::Copy::Recursive, you can use the following code to wedge in a progressbar, or whatever you want.
I added File::Find in there so that I could calculate the sum of all the file sizes, and then use that in my version of the progress bar.
N.B. I put the sleep command in there only for demo purposes, remove it for production code ;-)
use File::Find;
use File::Copy::Recursive qw(dircopy);
use strict;
use vars qw($dir_from $dir_to $size_all $size_remain *mycopy);
$|=1; # turn off buffering is needed in this case
# save the current version of copy that is used by F::C::R
*mycopy = *File::Copy::Recursive::copy;
# replace the copy func so we can wedge in a progressbar
*File::Copy::Recursive::copy = *mycopy_func;
# 1. call the original copy func
# 2. calculate remaining bytes
# 3. and call the show progress func
sub mycopy_func {
&mycopy(@_);
-f $_[0] and $size_remain -= -s $_[0];
mycopy_showprogress($size_remain);
}
# this is called after every file is copied
sub mycopy_showprogress {
my ($remaining) = @_;
printf "%s of %s remaining. \r",$size_remain, $size_all;
sleep 1; ##### FOR DEMO ONLY, REMOVE OTHERWISE
}
# setup the directories to copy
$dir_from = "/tmp/from";
$dir_to = "/tmp/to";
# Calculate the sum-total of the sizes of the files in the from dir
$size_all = $size_remain = 0;
find(sub {-f and $size_all+=-s;},$dir_from);
$size_remain=$size_all;
# Do it!
dircopy($dir_from, $dir_to);
# some visual cleanup
print "\n";
__END__
Here's another version that does not calculate the sum, rather it just prints the name of the file it is copying.
use File::Copy::Recursive qw(dircopy);
use strict;
use vars qw($dir_from $dir_to *mycopy);
$dir_from = "/tmp/from";
$dir_to = "/tmp/to";
sub mycopy_func {
&mycopy(@_);
mycopy_showprogress(@_); }
sub mycopy_showprogress {
my ($remaining) = @_;
printf "copying %s to %s. \r",@_;
sleep 1; ##### FOR DEMO ONLY, REMOVE OTHERWISE
}
$|=1;
*mycopy = *File::Copy::Recursive::copy;
*File::Copy::Recursive::copy = *mycopy_func;
dircopy($dir_from, $dir_to);
print "\n";
__END__
HTH!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.