Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ftp and unlinking

Status
Not open for further replies.

mbrohm

Programmer
Joined
Sep 18, 2002
Messages
5
Location
US
I am very new to perl and was wondering after I ftp a file is there a way to delete that file from the local machine without closing the session? i.e. can I

sub put {
my $this = shift;
my $rc = $this->connect();
if ( $rc ) {
$this->error( "Connect failed" );
return $rc;
}
foreach my $file ( @_ ) {
$rc = $FTP->put( $file );
if ( ! $rc ){
$this->error("Put of file failed: $file");
return "Put of file failed: $file";
}
$this->info("\nSuccessful put of file: $file");
unlink($file);
}
0;
}

So the remote machine has the file but the local machine does not, then close the session from a different function.
 
I'm not sure about FTP in Perl, but what you could do is build up an array of successful transfers, and process (delete) them after you have disconnected.

I don't know if I'd delete the files out of hand, but move them to a holding area (like a recycle bin), just in case the transfer wasn't successful.

Not the answer you were looking for, but might help...

;P
 
Sure you can:

require('ftplib.pl');
my $remote='172.16.170.1' ; #IP of remote machine
my $username='user'; #login name at $remote
my $passwd='none'; #the password at the remote
my $remotedir='/home/user/datadump'; #remote directory to put the files
ftp::open($remote, $username,$passwd) or print "cannot open $remote\n";#open ftp
ftp::binary(); #binary transfer mode
ftp::cwd($remotedir); #cd to remote directory
my $dir='.'; #current directory
opendir (DIR,"$dir") or die "cannot open $dir\n";
foreach my $file(readdir DIR){
next unless $file=~/$matchstring/; #this will only ftp files
# that match some string(unless you want to ftp all files in teh directory)
my $newname=$file; #here you can rename the file if you wish
my $success=1;
#ftp::put($file,$newname) or print "cannot put $file\n";
#check if this is ok I think it is, but you may need to
#reverse file and newname
unless (ftp::put($file,$newname) ){$success=0; print
"cannot put $file\n";} #!!! CHECK THIS, I have not tested it and you'd better be sure that if the transfer failed, you do not unlink!
unlink $file if $success==0;
}#end of foreach file loop
Hope this help, svar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top