Forum Moderators: coopster & phranque

Message Too Old, No Replies

listing directory size in ftp

I need to get a directory size from within a ftp session

         

bigshow

9:23 pm on Sep 25, 2005 (gmt 0)

10+ Year Member



I'm running a perl script from a unix server and ftp'ing to a windows server (it could be Windows 2000 or Windows 2003 server).

I need to get the size of a folder (including all it's sub-folders).

I've tried:

use Net::FTP;
my $username=xyz;
my $password=abcd;
my $directory="\temp";

$ftp = Net::FTP->new("servername") or die "Can't connect: $@\n";
$ftp->login($username, $password) or die "Couldn't login\n";
$dir_size=$ftp->size($directory) or die "Couldn't get directory size\n"
$ftp->quit() or warn "Couldn't quit.\n";
print "Size: $dir_size\n";

But it fails at the "Couldn't get directory size".

However, it works if i try size on a file: $file_size=$ftp->size($filename) or die "Couldn't get $filename size\n";

The man page for Net::FTP indicates you can only do a $ftp->size($filename) on a file not a directory.

I need to be able to get the directory size - any idea's how?

KevinADC

10:48 pm on Sep 25, 2005 (gmt 0)

10+ Year Member



not sure if this will help at all, but this line:

my $directory="\temp";

should be changed to:

my $directory='\temp';

\t inside double-quotes is a tab, not a backslash followed by a t.

KevinADC

10:51 pm on Sep 25, 2005 (gmt 0)

10+ Year Member



just noticed a missing semi-colon on the end of this line:

$dir_size=$ftp->size($directory) or die "Couldn't get directory size\n"

that should probably make the script crash so maybe just a typo when posting the code.

KevinADC

11:13 pm on Sep 25, 2005 (gmt 0)

10+ Year Member



this seems to work OK:



#!perl -w
use Net::FTP;
use strict;
use CGI qw/:standard/;
print header,start_html;
my $username='yourname';
my $password='yourpass';
my $directory='/public_html';
my $ftp = Net::FTP->new('yourhost') or die "Can't connect: $@\n";
$ftp->login($username, $password) or die "Couldn't login\n";
$ftp->cwd($directory);
my @ls = $ftp->ls();
my $dir_size = '0';
for(@ls) {
my $size = $ftp->size($_) or next;
$dir_size+=$size;
print "$_ ($size)<br>\n";
}
$ftp->quit() or warn "Couldn't quit.\n";
print "Total Size: $dir_size\n";
print end_html;

bigshow

7:07 am on Sep 26, 2005 (gmt 0)

10+ Year Member



syntax aside (I cut and pasted and removed / put in new lines)....

The code given by KevinADC only gives file sizes in the current directory. I require folder size and it's contents (including sub-folders and files)

bigshow

3:24 pm on Sep 26, 2005 (gmt 0)

10+ Year Member



Any help would be much appreciated...