tags:

views:

296

answers:

3

I have FTP Perl script and I want to make sure whether the file transfer is complete by checking the number of bytes that are transfered to the remote server is equal to the actual bytes of the file in the local server. How could i accomplish this?

Here's what I have so far:

 my $ftp = Net::FTP->new($host, Debug => 1)
 or die "Could not connect to '$host': $@";

 $ftp->login($user, $pw)
 or die sprintf "Could not login: %s", $ftp->message;

 $ftp->cwd($path)
 or die sprintf "Could not login: %s", $ftp->message;

 $ftp->ls;

 $ftp->binary;

 $ftp->get($file)
 or die sprintf "Could not login: %s", $ftp->message;
+3  A: 

from the docs, you can use size()

size ( FILE )

Returns the size in bytes for the given file as stored on the remote server.

NOTE: The size reported is the size of the stored file on the remote 
server. If the file is subsequently transferred from the server in ASCII
mode and the remote server and local machine have different ideas about
"End Of Line" then the size of file on the local machine after transfer
may be different.

Code:

my $host="127.0.0.1";
my $user="anonymous";
my $pw = "asdfsf";
my $path="pub";
my $file="file";
my $ftp = Net::FTP->new($host, Debug => 0) 
    or die "Could not connect to '$host': $@";

$ftp->login($user, $pw) or die sprintf "Could not login: %s", $ftp->message;
$ftp->cwd($path) or die sprintf "Could not login: %s", $ftp->message;
$ftp->binary;
print $ftp->size($file) or die sprintf "Could not login: %s", $ftp->message;
$ftp->quit();
ghostdog74
this is fine but i want to comapre the size of the file on local before transfer and size of the remote file after transfer.how could i do it?
Vijay Sarathi
perldoc -f stat
ghostdog74
+2  A: 
print "FTP size = ", $ftp->size($file), "\n";
print "Local size = ", (-s $file), "\n";
Ellidi
A: 

The print statement looks like it will print on the screen. How do I modify the print to write to a file so I can code this in a script and compare the file sizes with the script rather that just look at it in interactive mode? Please send any response to [email protected] Thank you for yoyur help,

Trent

Trent Alexander
well...this should probably be another question rather that an answer to the above question.
Vijay Sarathi
you could change the print statement to my $size_of_file = ftp->size($file). and now you have the file size in a variable.for a local file you simply use the -s flag.please refer some perl book to get a better idea.
Vijay Sarathi