2009-11-18 2 views
1

J'ai un script Perl FTP et je veux m'assurer que le transfert de fichiers est terminé en vérifiant que le nombre d'octets transférés sur le serveur distant est égal aux octets réels du fichier sur le serveur local. Comment pourrais-je accomplir cela?Comment puis-je vérifier la taille d'un fichier sur FTP en utilisant Perl?

Voici ce que j'ai jusqu'à présent:

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

$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; 

Répondre

6

de la documentation, vous pouvez utiliser la taille()

 
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': [email protected]"; 

$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(); 
+0

cela est bien beau, mais je veux comapre la taille du fichier sur local avant le transfert et la taille du fichier distant après r transfert. comment pourrais-je le faire? – Vijay

+0

perldoc -f stat – ghostdog74

3
print "FTP size = ", $ftp->size($file), "\n"; 
print "Local size = ", (-s $file), "\n"; 
+0

Cela ne fonctionnera pas sans mettre ftp en mode binaire: $ ftp-> binary; – Fa11enAngel

Questions connexes