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?
#!/usr/bin/perl -w
use CGI qw/:standard/;
use strict;
use Net::FTP;
print header,start_html;
my $username='xyz';
my $password='abcd';
my $directory='\temp';
my $ftp = Net::FTP->new("servername") or die "Can't connect: $@\n";
$ftp->login($username, $password) or die "Couldn't login\n";
$ftp->cwd($directory) or die "$!";
my @list = $ftp->dir($directory) or die "$!";
for (@list) {
print "$_<br>\n";
}
$ftp->quit() or warn "Couldn't quit.\n";
print end_html;
Content-Type: text/html; charset=ISO-8859-1
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"><head><title>Untitled Document</title>
</head><body>01-25-03 12:32PM <DIR> Acs32<br>
09-14-04 01:03AM <DIR> Archive<br>
09-27-05 01:08AM <DIR> asset_export<br>
07-05-05 08:16PM <DIR> ats_ye<br>
06-28-05 03:02PM <DIR> ats32<br>
06-08-04 08:50PM 961 atscopy.bat<br>
07-03-03 01:40PM <DIR> atseod<br>
08-05-04 04:52PM <DIR> cp_export<br>
09-27-05 11:40AM <DIR> data<br>
01-25-03 12:29PM <DIR> DDICT<br>
11-22-04 07:41PM <DIR> DOWNLOAD<br>
04-05-04 09:01AM <DIR> emcc_download<br>
08-12-02 05:39PM 40960 eu.dll<br>
09-27-05 08:45AM <DIR> EURO<br>
01-13-05 04:48PM <DIR> exis<br>
09-27-05 10:07AM <DIR> FAX<br>
06-08-04 08:49PM <DIR> fees<br>
06-02-05 08:18PM <DIR> FOXPRO25<br>
09-16-05 07:10PM <DIR> from_atlas<br>
05-26-05 03:46PM <DIR> imports<br>
02-23-05 06:04PM <DIR> install<br>
09-26-05 11:28PM <DIR> Logs<br>
01-25-03 01:08PM <DIR> posrec<br>
09-23-05 12:30AM <DIR> ps<br>
01-25-03 01:09PM <DIR> QUANTIX<br>
05-05-05 02:25PM <DIR> Replication Test<br>
09-27-05 10:57AM <DIR> REPORT<br>
01-25-03 01:10PM <DIR> Results<br>
01-25-03 01:10PM <DIR> SETUP32<br>
07-25-05 11:55AM <DIR> SIGNOFF<br>
09-27-05 04:30AM <DIR> TEMP<br>
09-27-05 04:30AM <DIR> trax<br>
07-27-04 05:06PM <DIR> Vis_sp6<br>
07-27-04 05:54PM <DIR> VISION32<br>
</body></html>$
You'll see files supply a size, but not folders.
use Net::FTP;
my $username=xyz;
my $password=abcd;
my $directory='\temp';
my $ftp = Net::FTP->new("servername") or die "Can't connect: $@\n";
$ftp->login($username, $password) or die "Couldn't login\n";
my $total_size = '0';
$total_size = get_space($directory);
sub get_space {
local $_ = shift;
#print "<b>$_</b><br>";
$ftp->cwd($_) or die "can't change to directory '$_': $!";
my @list = $ftp->dir($_) or next;
foreach my $line (@list) {
my @f = split(/\s+/,$line);
if (index($line,'<DIR>') > -1) {get_space("$_/$f[-1]")}
else {$total_size += $ftp->size($f[-1])}
}
return($total_size);
}
$ftp->quit() or warn "Couldn't quit.\n";
print "Total Size: $total_size\n";
print end_html;
05-05-05 02:25PM <DIR> Replication Test
the line is split into an array which contains:
05-05-05
02:25PM
<DIR>
Replication
Test
the last element of the array is used as the foldername or filename, in this case only "Test" would get used and the cwd() function would fail if there is no folder named "Test". I failed to notice that this condition could exist. Normally spaces in names for internet files/folders is a no-no. I'll see what I can come up with though.
#!perl
use CGI qw/:standard/;
use strict;
use Net::FTP;
print header;
my $username='xyz';
my $password='abcd';
my $directory='\temp';
my $ftp = Net::FTP->new("servername") or die "Can't connect: $@\n";
$ftp->login($username, $password) or die "Couldn't login\n";
my $total_size = '0';
$total_size = get_space($directory);
sub get_space {
local $_ = shift;
#print "<b>$_</b><br>";
$ftp->cwd($_) or die "can't change to directory '$_': $!";
my @list = $ftp->dir($_) or next;
foreach my $line (@list) {
chomp $line;
if (index($line,'<DIR>') > -1) {
my $name = substr $line,index($line,'<DIR>')+6;
#print " $name<br>\n";
get_space("$_/$name");
}
else {
my $name = (split(/\s+/,$line))[-1];
#print " $name<br>\n";
$total_size += $ftp->size($name);
}
}
return($total_size);
}
$ftp->quit() or warn "Couldn't quit.\n";
print "Total Size: $total_size\n";
print end_html;
the problem with this script is that it is not portable at all. It will only work on a server that returns a list formatted like yours, with <DIR> followed by the folder name. My ftp server returns a differently formatted string, like this:
drwxr-xr-x 2 e-pixsco e-pixsco 4096 Jun 22 2003 bryce4
drwxr-xr-x 3 e-pixsco e-pixsco 4096 Sep 25 15:53 cgi-bin
-rw-r--r-- 1 e-pixsco e-pixsco 7472 Mar 23 2003 colors.html
-rw-r--r-- 1 e-pixsco e-pixsco 18524 Mar 23 2003 colors2.html
so if you hope to use this script on more than one server it may not work.
I also wish this forum formatted code better. hint-hint to the admins :)
However, you can format code somewhat. Anytime you are creating or replying to a message you will notice a link to the left of the dialog box that tells you that Style Codes [webmasterworld.com] are on. I use the [pre] tags most often, but you have to make sure you don't have blank lines in between your lines anywhere and that your lines don't extend too far (wrap) as they are not going to wrap ... simply make the sidescroll widen out on your post.
The code style tags are almost useless, just seems to change the font style but not preserve any formatting at all. Oh well, can't have everything!