Ummm...ok. I'll try to put it in a better form... REALLY UNTESTED AND SLOPPY CODE FOLLOWS:

#!/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


In reply to Re: Re: Re: use (of) Net::FTP for updating by antirice
in thread use (of) Net::FTP for updating by atnonis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.