Janelle
Janelle

Reputation: 67

Why can't I get a file's size with Net:FTP?

I cannot get a file's size with Net::Ftp. The error I receive is "file1.csv: No such file or directory." I'm sure the file exists. $ftp->supported('size') returned true. Using the full path ('/otherdir/file1.csv') results in the same error. There are no sub directories in the directory I am using & all of the files have a size >0. What am I doing wrong? I've stripped my code down to the following snippet -

#! /usr/bin/perl
use strict;
use warnings;
use Net::FTP;

my $ftp = Net::FTP->new("host", Debug =>0) or die "couldnt connect: $@";
$ftp->login("username","password") or die "couldnt login: ", $ftp->message;
$ftp->binary;
$ftp->cwd("otherdir") or die "couldn't cwd ", $ftp->message;
my @ftp_files = $ftp->ls();
print scalar(@ftp_files);

foreach  (@ftp_files){
  print $_,"\n";
  my $size= $ftp->size($_) or die $ftp->message;
  print $size,"\n";
}

Upvotes: 1

Views: 479

Answers (1)

mike jones
mike jones

Reputation: 659

size() only works for files. Not folders. Your code is probably die-ing because it gets a $size of undef for a folder. It never reaches the files that are there.

Upvotes: 1

Related Questions