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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NET:FTP delete method 1

Status
Not open for further replies.

plotzer

Programmer
Aug 28, 2001
35
US
I am trying to delete some files from an ftp directory using the delete method. I've create a list array of all the files in the directory and I'm trying to loop through all files and delete them. FOr some reason I am unable to do this. I actually hard coded one of the filenames into the method and it still didnt work. Is there a special way the filename needs to formated before it can be deleted? Attached is my code:

use Net::FTP;

$ftp = Net::FTP->new("s4", Debug => 0)|| die "Could not open FTP COnnection: $!\n";
$ftp->login('obs','abc')|| die "Could not login: $!\n";
$ftp->cwd("/export/home")|| die "Could not open home directory: $!\n";
(@list1 = $ftp->ls("/export/home"))|| die "LS Failed: $!\n";

$i = 0;
while($i < scalar(@list1))
{
$i++;
print &quot;Deleting $list1[$i]\n&quot;;
$ftp->delete($list1[$i])|| die &quot;Delete Failed: $!\n&quot;;
}

$ftp->quit;





Thanks in advance
 
Do you mean to skip the first file in the list? Perl arrays are zero-based so in your while loop when you increment $i BEFORE you call delete you skip the zeroth element of @list1 and delete the first element. The zeroth element never gets deleted. Another consequence of putting $i++ at the beginning of the loop is that you increment $i beyond the last index of the array and then access the array (remember that scalar(@list1) is the number of element in the array, NOT the index of the last value, which is $#list1). this will throw an error about uninitialized values when you do get the code to work.

If you insist on using that atrocious loop syntax then you should put the $i++ at the end of the loop. Or if you really do mean to skip the first element then shift() it off before the loop begins or initialise $i to 1.

A prettier (and more Perlish) way to loop over the elements of @list1 would be like

foreach $file ( @list1 )
{
print &quot;Deleting $file\n&quot;;
$ftp->delete($file) || die &quot;Delete Failed: $!\n&quot;;
}

no messing with indecies when it's not necessary and no worrying about array bounds.

As for why your code isn't working, I ran what you posted and it worked fine so your paths must be incorrect or something else is wrong. What do you mean that you are &quot;unable to do this&quot; and &quot;it still didn't work&quot;? What are the error messages that you get? What, if any, is the output? Add a

print &quot;@list\n&quot;;

before the loop. Does it output what you expect?

jaa

 
Thanks much

You are correct, my loop was atrocious. Your code snippet worked perfectly.

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top