Perl File Copy Progress Bar

craigmp

n00b
Joined
May 11, 2005
Messages
45
Right now I have a small perl program that copies an access database from a server to my local computer and then compresses it into a rar file. I was wondering if there is a way to add a progress bar for the file copy part of the program. It is a rather large database and takes a some time to copy it. The rar part shows a percentage already.
 
how are you copying the files? is the filesystem mounted are are you retrieving it via ftp or something?

if the server's filesystem is mounted you could do a

@remote_filename_stats = stat($remote_filename);

$remote_filename_stats[7] would hold the file's total size in bytes. then after you begin copying the file you can stat() the local file on given intervals, and compare the 2 size values to derive a percentage. ie:

$percent_complete = $local_filename_stats[7] / $remote_filename_stats[7] * 100

for a status bar you could just scale an arbitray number of "*" or whatever to reflect the percentage complete, using clear() commands to wipe the console display so you can keep updating it, or do something a little more fancy with ncurses or something
 
you're either going to have to find a perl module that has a spiffy copy procedure in it, or write your own. If you try to write your own, you'll probably find it's not really worth it. Of course, what you could do is in one thread copy it and in another thread (if perl allows such activites) look at the local filesize, and compare that to the remote filesize.
 
yah, perl has threading and forking. but if he executes `cp &` as a system() command it should run in the background while the script can continue polling both files till the transfer is complete. assuming this is *nix anyway
 
I guess I should have said that this is using Active State Perl on a Windows XP machine. I'm using File::Copy to copy the files from a Windows 2003 server to my machine, add a date to the file name and then that file is added to a win rar archive using Archive::Rar.. I'm new to perl and I was thinking about comparing file sizes and I have read some things about threading and forking but its all new to me. I looked on google for a file::copy that had some type of progress bar built in but I really couldn't find anything. The only thing I found on cpan was Acme::progressBar but it has to do the copy first to calculate the time and then it copies the file. This takes twice as long as it normally would. Maybe I'll just have to live with my "Please Wait" message. This is just something I quickly put together one day when I was trying to learn some basic perl. My only concern is if someone else runs this program they won't think its working because nothing is being displayed on the screen.
 
Back
Top