The following is the test code I wrote. It should be useful in testing. It is not "short". It is written top down so you can comment out a one liner at the bottom to remove a ton of testing associated with that one liner.
It is also "self contained", so you don't have to open a shell on the server and set up test files and directories
And I have commented out all the stuff that works.
<code>
# RotatorForCobianBackupFTP.pl
# configure the following three to your location
my $Username = "xxxx";
my $Password = "yyyy";
my $FtpServer = "aaa.bbb.ccc.ddd";
my $BkRoot = ".metadata";
my $DotFile = ".lock";
my $DotDir = ".precomp";
my $SomeFile = "eraseme";
use strict;
use warnings;
use diagnostics;
# Reference for Net::FTP
#
http://search.cpan.org/~shay/libnet-3.10/lib/Net/FTP.pm
use Net::FTP;
my $ftp;
sub lsa ( $$ ) {
# do a full directory (ls -a) and return a reference pointer to a results array
# print if $Print > 0
my $DirPath = $_[0];
my $Print = $_[1];
my @Dir;
@Dir = @{$ftp->ls ( "$DirPath/.*" )};
push ( @Dir, @{$ftp->ls ( "$DirPath" )});
if ( $Print > 0 ) {
print "Directory Listing of $DirPath\n";
for my $Line ( @Dir )
{ print" $Line\n"; }
print "\n";
}
return \@Dir;
}
sub Exists ( $$$ ) {
# Test an FTP directory path for the existance of a file/directory
my $DirPath = $_[0];
my $HuntFor = $_[1]; # This must be the last thing in the string
my $Print = $_[2]; # print if > 0
my @DirArray;
# print STDERR "Exsits Path = <$DirPath> HuntFor = <$HuntFor>\n";
@DirArray = @{lsa ( "$DirPath", 0 ) };
# for my $Line ( @DirArray) { print" Exists Line = <$Line>\n"; }
for my $Line ( @DirArray ) {
# print " Exists Line = <$Line>\n";
if ( $Line =~ /${HuntFor}$/ ) {
if ( $Print > 0) { print " Exists: $HuntFor was found in $DirPath\n\n"; }
return 1; }
}
if ( $Print > 0 ) { print " $HuntFor was not found in $DirPath\n\n"; }
return 0;
}
sub RecreateTestFiles () {
# create what was forgotten
# if $BkRoot does not exist, create it
my $FileHandle;
if ( not Exists ( "/", $BkRoot, 0 ) )
{ print "Recreating $BkRoot directory\n";
$ftp->mkdir ( $BkRoot ); }
if ( not Exists ( "/", $DotDir, 0 ) )
{ print "Recreating $DotDir directory\n";
$ftp->mkdir ( $DotDir ); }
if ( not Exists ( "/$BkRoot", "$DotFile", 0 ) ) {
print "Recreating $BkRoot/$DotFile file\n";
if ( not -f "$DotFile" ) {
print "Recreating local $DotFile file\n";
open $FileHandle, ">>", "$DotFile";
close $FileHandle;
}
$ftp->put ( "$DotFile", "/$BkRoot/$DotFile" );
}
if ( not Exists ( "/$BkRoot", "$SomeFile", 0 ) ) {
print "Recreating $BkRoot/eraseme file\n";
if ( not -f "eraseme" ) {
print "Recreating local $SomeFile file\n";
open $FileHandle, ">>", "$SomeFile";
close $FileHandle;
}
$ftp->put ( "eraseme", "/$BkRoot/$SomeFile" );
}
lsa ( "/", 1);
lsa ( "/$BkRoot", 1);
print "\n";
}
sub RecurseRmDir ( $ ) {
my $Target = $_[0]; # Stone age subs
print "Attempting to recursively delete direcory $Target\n";
if ( $ftp->rmdir ( "$Target", 1 ) )
{ print "Directory $Target was recursively removed\n"; }
else
{ print "Recursive removal of $Target failed\n"; }
lsa ( "/$BkRoot", 1);
print "\n";
}
sub DelFile ( $ ) {
my $Target = $_[0]; # Stone age subs
print "Attempting to delete file $Target\n";
if ( $ftp->delete ( "$Target" ) )
{ print "File $Target was deleted by ftp->delete\n"; }
else
{ print "File $Target failed to delete with ftp->delete\n"; }
lsa ( "/$BkRoot", 1);
print "\n";
}
sub RenameFile ( $ ) {
my $Target = $_[0]; # Stone age subs
print "Attempting to rename file $Target\n";
if ( $ftp->rename ( "$Target", "$BkRoot/dotfile" ) )
{ print "File $Target was renamed to $BkRoot/dotfile\n"; }
else
{ print "File $Target failed rename to $BkRoot/dotfile\n"; }
lsa ( "/$BkRoot", 1);
print "\n";
}
sub RmDotDir () {
print "Attempting to remove empty dot directory <$DotDir>\n";
$ftp->rmdir ( "$DotDir" );
lsa ( "/", 1 );
}
print "My version of Net::FTP is " . $Net::FTP::VERSION . "\n\n";
# Open a new FTP connnector
$ftp = Net::FTP->new("$FtpServer", Passive=>1 )
or die "Cannot connect to $FtpServer: $@";
# Log on to the FTP site
$ftp->login("$Username", "$Password")
or die "Cannot login ", $ftp->message;
RecreateTestFiles;
# DelFile ( "/$BkRoot/$DotFile" );
# RecreateTestFiles;
# RenameFile ( "/$BkRoot/$DotFile" );
# RecreateTestFiles;
RecurseRmDir ( "/$BkRoot" );
# RecreateTestFiles;
# RmDotDir;
$ftp->quit;
# With this, I will say no more
__END__
</code>
C:\NtUtil>perl Net.FTP.Delete.Test.pl
My version of Net::FTP is 3.10
Recreating .precomp directory
Directory Listing of /
/.
/..
/.metadata
/.precomp
/IAmMatt
/MyDocsBackup
Directory Listing of /.metadata
/.metadata/.
/.metadata/..
/.metadata/.lock
/.metadata/eraseme
Attempting to recursively delete direcory /.metadata
Recursive removal of /.metadata failed
Directory Listing of /.metadata
/.metadata/.
/.metadata/..
/.metadata/.lock