Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Copying a directory and its contents while displaying a status

by hiseldl (Priest)
on Aug 04, 2008 at 22:13 UTC ( #702192=snippet: print w/replies, xml ) Need Help??

Uses File::Copy::Recursive, but wedges another 'copy' sub so that a progress bar, or some other hook, can be displayed or run.


The real trick to this particular snippet is determining that File::Copy::Recursive uses File::Copy::copy, but the copy sub is imported into the File::Copy::Recursive namespace rather than its own namespace. If you try to hook File::Copy::copy, it will not work.

For completeness, thank you jdporter, here is what it would look like if Hook::LexWrap was used:

use Hook::LexWrap; use File::Copy::Recursive qw(dircopy); use strict; use vars qw($dir_from $dir_to); $dir_from = "/tmp/from"; $dir_to = "/tmp/to"; $|=1; # Using Hook::LexWrap my @dirs; wrap *File::Copy::Recursive::copy, pre => sub { @dirs = @_ }, post => sub { printf "copying %s to %s. \r", @dirs }; dircopy($dir_from, $dir_to); print "\n";
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 {
    # call the original

    # call my sub after

sub mycopy_showprogress {
    # this could call anything to show progress or even
    # to operate on the file being copied
    printf "copying %s to %s. \r",@_;


# Add the hook
*mycopy = *File::Copy::Recursive::copy;
*File::Copy::Recursive::copy = *mycopy_func;

dircopy($dir_from, $dir_to);
print "\n";
Replies are listed 'Best First'.
Re: Copying a directory and its contents wihile displaying a progress bar
by jdporter (Chancellor) on Aug 05, 2008 at 11:35 UTC

    Or you could use one of the Hook:: modules from CPAN, such as Hook::LexWrap

    Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.
Re: Copying a directory and its contents while displaying a progress bar
by ambrus (Abbot) on Aug 06, 2008 at 10:38 UTC


    Really wow.

    This does work, but at first it really seemed to me like it couldn't work. I didn't understand it yesterday, didn't understand it today, even downvoted it, got suspicious when I saw its noderep, tried to write a reply where I argue how this can't possibly work, and only understood how it could work during that. So I downloaded the cpan module, tested it, and it worked.

    It might have been better if you wrote a bit more comments about it.

    For anyone else puzzled, this overrides a private function from the File::Copy::Recursive cpan module. That function is called recursively on each file so this overriding code can print a debug output saying what file is copied. The title is confusing, because it doesn't actually display a progress bar, only prints the name of each file as it's copied. You use the code by calling the dircopy function with a source and target directory name.

    Update: By the way, I won't use this code, because I don't seem to ever need copying files recursively from a script, and because cp -av works well enough.

      Thank you for the feedback. I have changed the title a bit, so it is not so misleading, and I have added more comments about this snippet to include an example using Hook::LexWrap.

      An example problem that this is a solution for, is that a user wants to plug in their USB drive and they want to copy their files to their PC. So, to be user friendly, you could use this snippet as a basis for showing progress while files are getting copied. You could insert Tk progressbars, show remaining bytes, etc.

      What time is it? It's Camel Time!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://702192]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2022-01-18 20:33 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (54 votes). Check out past polls.