Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Copying a directory and its contents wihile displaying a progress bar

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


in reply to Copying a directory and its contents wihile displaying a progress bar

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!


Comment on Re: Copying a directory and its contents wihile displaying a progress bar
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-12-22 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (114 votes), past polls