Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Delete files from server after downloading using SFTP

by Anonymous Monk
on Jun 11, 2013 at 18:09 UTC ( #1038307=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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!

Comment on Delete files from server after downloading using SFTP
Select or Download Code
Re: Delete files from server after downloading using SFTP
by hbm (Hermit) on Jun 11, 2013 at 21:15 UTC

    I recommend sleep-and-retry, for that inevitable transmission problem:

    sub removeRemoteFile { my $file = shift; for (1 .. 5) { return TRUE if $sftp -> remove($file); sleep 30; } return FALSE; }

    Actually, I tend to pass a hash of configuration details (hostname, username, retries, sleep time, etc.) and do this:

    sub removeRemoteFile { my %args = @_; for (1 .. $args{RETRIES}) { return TRUE if $sftp -> remove("$args{FILE}"); sleep $args{SLEEP}; } return FALSE; }

    I do a similar sleep and retry for any get and put, but also compare remote and local file sizes:

    sub getFile { my %args = @_; my $name = $args{FILE}; $name =~ s|.*/||; for (1 .. $args{RETRIES}) { my $size; if ($sftp -> get($args{FILE},"$args{LOCAL}/$name")) { $size = -s "$args{LOCAL}/$name"; } return TRUE if defined $size && $size == $args{RSIZE}; sleep $args{SLEEP}; } unlink "$args{LOCAL}/$name" if -f "$args{LOCAL}/$name"; return FALSE; }

    When removing files, I do so IF the respective put or get is successful. Something like:

    for my $file ( ... ) { if (getFile( ... )) { if (!removeRemoteFile( ... )) { warn "Couldn't remove remote $file!\n" } } else { warn "Couldn't download $args{REMOTE}/$file!\n"; } }
Re: Delete files from server after downloading using SFTP
by salva (Monsignor) on Jun 12, 2013 at 07:50 UTC
    my $sftp = new Net::SFTP::Foreign(host => $host, user => $user, passwo +rd => $pwd, more => '-v', autodie => 1); $sftp->setcwd('/directory'); my $files = $sftp->glob('*.txt', names_only => 1); for my $file (@$files) { $sftp->get($file, "/in_dir/$file", numbered => 1); $sftp->remove($file); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1038307]
Approved by kcott
Front-paged by perlfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-09-23 07:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (211 votes), past polls