Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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!

In reply to Delete files from server after downloading using SFTP by Anonymous Monk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (8)
    As of 2018-06-20 20:11 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (117 votes). Check out past polls.