http://qs1969.pair.com?node_id=44971


in reply to File copy progress.

The code prints out that it has done 100% twice ;-) this is probs bad.. Hows about changing it to show the bytes read / bytes left to read. This would also allow you to change the block size for larger files. The simplest way to do this would be to incr a variable after the sysread and print that rather than the percentage. I could not see why it was printing 100% twice but I did not look hard.

On a stylistic point, why did you use a sub to print out the message, and why did you pass that sub as an arg? Just interested to hear your rational.

Nice little bit of code, ++, I have added it to my personal repository of bits of useful stuff .. nice one. --

Zigster

Replies are listed 'Best First'.
Re: Re: File copy progress.
by zzspectrez (Hermit) on Dec 05, 2000 at 19:00 UTC

    Yeah.. I know it prints 100% twice. It only does that if the file doesnt evenly divide by 10. This code was a quick hack of 15 minutes to see if the idea would work. I still need to mess with it to make it work better.

    The reason why I pass a subroutine as a variable to the copy routine is just so I could have the calling program determine how to display the status. That way I could just redefine that subroutine and have it display an actually prograss bar or something..

    zzSPECTREz

      Sweet, a progress bar would be a nice touch.
      --

      Zigster

        Here is quick but cheap way to do it. Im just posting the progress routine but in the main routine you also have to toss the percent variable and instead have a variable keep track of total bytes writen. Then call this sub passing it total_writen, and file size. This routine also uses fastolfe's suggestion of using \r instead of \n so just the line is updated ( works on win2k too!).

        sub FileProgress () { my $wrote = shift; my $size = shift; my $percent = $wrote / $size * 100; case: { if ($percent <11) { print "\r[* ]"; last;} if ($percent <21) { print "\r[** ]"; last;} if ($percent <31) { print "\r[*** ]"; last;} if ($percent <41) { print "\r[**** ]"; last;} if ($percent <51) { print "\r[***** ]"; last;} if ($percent <61) { print "\r[****** ]"; last;} if ($percent <71) { print "\r[******* ]"; last;} if ($percent <81) { print "\r[******** ]"; last;} if ($percent <91) { print "\r[********* ]"; last;} if ($percent <=100) { print "\r[**********]"; last;} } printf " %3u",$percent; print '%'; printf " done. %u bytes.",$wrote; return 1; }

        zzSPECTREz