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!

--
hiseldl
What time is it? It's Camel Time!


In reply to Re: Copying a directory and its contents wihile displaying a progress bar by hiseldl
in thread Copying a directory and its contents wihile displaying a progress bar by bittis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.