Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Good evening fellow monks!!

I have this code I have been using that copies files between windows computers on a local network. Some of the files are quite large (100+megs) and can take up to 2 minutes to transfer. This is no problem, but sometimes leaves you wondering if your program is still running or has it crashed. So I was wondering how I could implement some kind of copy progress status.

The following code is what I came up with. What do the fellow monks think? Any suggestions? Improvements?

#!/usr/bin/perl -w use strict; sub CopyFileProgress ( ) { my $src = shift; my $dst = shift; my $callback = shift; my $num_read; my $num_wrote; my $buffer; my $perc_done = 0; open (SRC, "< $src") or die "Could not open source file [$src]: $! +\n"; open (DST, "> $dst") or die "Could not open destination file [$dst +]: $!\n"; binmode SRC; binmode DST; my $filesize = (-s $src) or die "File has zero size.\n"; my $blksize = int ($filesize / 10); while (1) { $num_read = sysread(SRC, $buffer, $blksize); last if ($num_read == 0); + die ("Error reading from file [$src]: $!\n") if (!defined($num +_read)); my $offset = 0; while ($num_read){ $num_wrote = syswrite(DST,$buffer,$num_read,$offset); die ("Error writing to file [$dst]: $!\n") if (!defined($n +um_wrote)); $num_read -= $num_wrote; $offset += $num_wrote; } $perc_done += 10 unless $perc_done == 100; &$callback($perc_done) or die ("Copy canceled.\n"); } } sub FileProgress () { my $percent = shift; print "$percent% done\n"; return 1; } if (@ARGV == 2) { my $source = shift @ARGV; my $destination = shift @ARGV; &CopyFileProgress($source,$destination,\&FileProgress); }else{ print "\n\nUSAGE: copy.pl <source filename> <destination filename> +\n\n"; }

Later....
zzSPECTREz


In reply to File copy progress. by zzspectrez

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2023-06-03 17:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (16 votes). Check out past polls.

    Notices?