Todd Chester has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl Monks,

I know I am being picky here and that I could just do two ftp->dir's twice and combine the arrays, but it is bugging me none the less.

I want to create an array that contains the contents of an ftp directory.    This is what it looks like in bash:

MyDocsBackup]$ ls -al backup1 total 12 -rw-r--r--. 1 matt users 0 Feb 5 22:06 .eraseme -rw-r--r--. 1 matt users 0 Feb 5 22:11 eraseme2 drwxr-xr-x. 2 matt users 4096 Feb 5 22:19 eraseme.dir -rw-r--r--. 1 matt users 0 Feb 5 22:16 eraseme.too

You will notice the first entry starts with a dot (".").

This catches the one with the starting dot:
my @TestArray = @{$ftp->dir ( "/$BkRoot/backup1/.*" )}
But not anything else.

These two catch everything else, but not the one with the dot:
my @TestArray = @{$ftp->dir ( "/$BkRoot/backup1/*" )} my @TestArray = @{$ftp->dir ( "/$BkRoot/backup1" )}

Mumble, mumble.  Is there a way to get ftp->dir to give me everything the first time?

Many thanks,
-T


This is what I have been using:
print "/$BkRoot/backup1 = \n"; my @TestArray = @{$ftp->dir ( "/$BkRoot/backup1/.*" )}; push ( @TestArray, @{$ftp->dir ( "/$BkRoot/backup1" )}); for ( @TestArray ) { print " $_\n"; }

Replies are listed 'Best First'.
Re: Need Net::FTP::dir help
by GotToBTru (Prior) on Feb 06, 2017 at 16:05 UTC

    Informal testing suggests:

    $ftp->cwd("/$BkRoot/backup/"); my @TestArray=$ftp->dir;

    Update: per suggestion from choroba, de-referencing is not needed.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: Need Net::FTP::dir help
by Todd Chester (Scribe) on Feb 10, 2017 at 00:20 UTC
    This is what I came up with;
    sub GetDirHashPtr ( $ ) { # Returns a reference pointer to an array of hashes: # 'Type'=>"Directory|File" # 'Name"=>varies my $DirPath = $_[0]; my @DirArray; my @DirArrayOfHashes; # print "Making directory of $DirPath\n"; @DirArray = @{$ftp->dir ( "$DirPath/.*" )}; push ( @DirArray, @{$ftp->dir ( "$DirPath" )}); # for ( @DirArray ) { print " $_\n"; }; print "\n"; for my $Line ( @DirArray ) { # note: you want everything else at the end my @SplitArray = split ( /\s{1,}/, $Line, 9 ); # print "<$SplitArray[0]> <$SplitArray[8]> \n"; if ( "$SplitArray[8]" eq "\." || "$SplitArray[8]" eq "\.\." ) { # print "Skipping $SplitArray[8] directories\n"; } elsif ( $SplitArray[0] =~ /^d/ ) { # print "Pushing Directory $SplitArray[8]\n"; push ( @DirArrayOfHashes, { 'Type'=>"Directory", 'Name'=>"$Sp +litArray[8]" } ); } else { # print "Pushing File $SplitArray[8]\n"; push ( @DirArrayOfHashes, { 'Type'=>"File", 'Name'=>"$SplitAr +ray[8]" } ); } } # print "\n"; # for my $LinePtr ( @DirArrayOfHashes ) { # print "Type = $$LinePtr{'Type'} Name = $$LinePtr{'Name'}\n"; # }; # print "\n"; return \@DirArrayOfHashes; }