Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Copying a directory and its contents while displaying a status

by hiseldl (Priest)
on Aug 04, 2008 at 22:13 UTC ( [id://702192]=CUFP: 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.

update:

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 &mycopy(@_); # call my sub after mycopy_showprogress(@_); } 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",@_; } $|=1; # 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 (Paladin) 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

    Wow.

    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.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-03-28 21:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found