#!/usr/bin/perl -w
use strict;
use Net::FTP;
# change preferences to meet your needs
my $ftp_srv = 'ftp.ntua.gr';
my $ftp_usr = 'anonymous';
my $ftp_pass = 'me@somewhere.com';
my $ftp_path = '/pub/linux/slackware/slackware-9.0/';
# the main program
my $con;
$con = Net::FTP->new($ftp_srv, Debug => 1)
or die "Unable to connect to $ftp_srv\n". $con->message ."\n";
$con->login($ftp_usr,$ftp_pass)
or die "Cannot login: ". $con->message ."\n";
$con->cwd($ftp_path)
or die "Cannot change to $ftp_path dir\n".$con->message ."\n";
my %dir = %{parse_dir('.')};
$\ = "\n";
print "Outputting directory .";
print $_,"\t"x2,$dir{$_} foreach sort keys %dir;
my $refarr = separate_file_dir(%dir);
print "Printing dirs ======================";
print foreach @{$refarr->{'dir'}};
print "Printing files =====================";
print foreach @{$refarr->{'file'}};
print "Printing links =====================";
print foreach @{$refarr->{'link'}};
# pass this the directory you want to get the listing from
sub parse_dir {
shift;
my @ls = $con->dir($_)
or die "Cannot perfom ls command\n".$con->message."\n";
my %dir;
$dir{$_->[0]} = $_->[1] foreach (map parse_line($_), @ls);
\%dir;
}
# separate the dir's and files to two arrays
# send the entire hash to this baby
sub separate_file_dir {
my %hash = @_;
my $arr1 = [];
my $arr2 = [];
my $arr3 = [];
foreach (sort keys %hash) { # yes, I know this is bad :-/
push @$arr1, $_ if $hash{$_} eq 'dir';
push @$arr2, $_ if $hash{$_} eq 'file';
push @$arr3, $_ if $hash{$_} eq 'link';
}
{ dir=> $arr1, file=> $arr2, link => $arr3 };
}
sub parse_line {
shift;
/^(.).*\s([^\s]*)$/;
if ($1 eq 'd') { $a = "dir"; } # yes, I used $a, I suck...blah blah
+blah
elsif ($1 eq 'l') { $a = "link"; }
else { $a = "file"; }
[$2,$a];
}
Ok. This code gives you two very nice subs called parse_dir which takes a string representing the directory to check and returns a hashref for which the filenames are keys and the values are dir, link, or file. separate_file_dir takes a hash as its input and returns a hashref with the keys dir, file, and link. Each value is a sorted arrayref of the associated directories, files, or links. Please note that no error checking is present for these subs. If anyone is horrified by this messy code, I apologize :-)
antirice The first rule of Perl club is - use Perl The ith rule of Perl club is - follow rule i - 1 for i > 1 |