Hi there Monks!
I am trying to delete all files from the server once downloading is completed.
My question is if this example is the best way:
# Delete a file on the server:
my $success = $sftp->RemoveFile("test.txt");
if ($success != 1) {
print $sftp->lastErrorText() . "\n";
exit;
}
print "Files Deleted!" . "\n";
This is more of the code I am using:
...
# For debugging purposes you can run ssh in verbose mode passing it
+the -v option:
my $sftp = new Net::SFTP::Foreign(host => $host, user=>$user, passwo
+rd=>$pwd, more => '-v');
$sftp->die_on_error("unable to connect to remote host");
$sftp->setcwd('/directory') or die "unable to change cwd: " . $sftp-
+>error;
print $sftp->cwd . "\n";
my $files = $sftp->ls('/directory', no_wanted => qr/^\./);
# get files now
$sftp->mget('*.txt', "/in_dir", no_wanted => qr/^\./, numbered => 1)
+ or die "mget failed: " . $sftp->error;
#print "$_->{filename}\n" for @$files;
#print "$_->{longname}\n" for @$files;
my @all_files;
foreach my $files (@$files) {
next unless ($files->{filename} =~ m/\.txt$/);
push @all_files, $files->{filename};
}
if ( !$@ ) {
print "\n Transmission complete...\n";
# Delete a file on the server:
my $success = $sftp->RemoveFile("test.txt");
if ($success != 1) {
print $sftp->lastErrorText() . "\n";
exit;
}
print "Files Deleted!" . "\n";
}else {
print "\n File download failed, try later.\n";
}
$sftp->disconnect;
do_other_stuff(\@all_files);
...
Thanks for the Help!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.