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

Perl Script Delete Problem

Status
Not open for further replies.

jonance

Technical User
Nov 8, 2001
4
0
0
US
Hi,

I just joined the board and have been slooooowly learning perl. I installed smartsearch.cgi on my site at The platform is Windows NT. It is basically just a pay per click search engine program. Well it works great but in the admin section there is a option to delete a user. When an account is created, a directory is created name after their username, and inside that a directory is created called "logs". Here is the code inside admin.pl, the admin script, that is apparent supposted to be deleting the files and folders. The files inside the "username" directory are all deleted correctly when i hit 'delete' but the directories (username,logs) aren't deleted. I would appreciate any help and would be glad to give you ftp info if you want to look deeper into the script.

Thanks,
John

Here is the code:

------------------------------------------------
sub deleteuser {
open(DATA,"$add_ntpath\\accounts\\$delete\\balance.data");
$balance = <DATA>;
close(DATA);
if($balance > 0.01) {
open(DATA,&quot;$add_ntpath\\accounts\\$delete\\bids.data&quot;);
@bids = <DATA>;
close(DATA);
foreach $bid(@bids) {
@biddata = split(/&&/, $bid);
@newlist_b = &quot;&quot;;
open(DATA,&quot;>$add_ntpath\\keywords\\$biddata[0]&quot;);
flock (DATA,2);
@keydata_b = <DATA>;
flock (DATA,8);
close(DATA);
foreach $key(@keydata_b) {
@kdata_b = split(/&&/, $key);
if($kdata_b[2] ne $delete) { push(@newlist_b,$key); }
}
open(DATA,&quot;>$add_ntpath\\keywords\\$biddata[0]&quot;);
flock (DATA,2);
print DATA @newlist_b;
flock (DATA,8);
close(DATA);
}
}

unlink(&quot;$add_ntpath\\accounts\\$delete\\bids.data&quot;);
unlink(&quot;$add_ntpath\\accounts\\$delete\\balance.data&quot;);
unlink(&quot;$add_ntpath\\accounts\\$delete\\info.data&quot;);
unlink(&quot;$add_ntpath\\accounts\\$delete\\lowsent&quot;);

opendir (DIR, &quot;$add_ntpath\\accounts\\$delete\\logs\\aff&quot;);
@afiles = grep { /.aff/ } readdir(DIR);
close (DIR);
foreach $files(@afiles) { unlink(&quot;$add_ntpath\\accounts\\$delete\\logs\\aff\\$files&quot;); }
rmdir(&quot;$add_ntpath\\accounts\\$delete\\logs\\aff&quot;);

opendir (DIR, &quot;$add_ntpath\\accounts\\$delete\\logs&quot;);
@files = grep { /.log/ } readdir(DIR);
close (DIR);
foreach $files(@files) { unlink(&quot;$add_ntpath\\accounts\\$delete\\logs\\$files&quot;); }
rmdir(&quot;$add_ntpath\\accounts\\$delete\\logs&quot;);
rmdir(&quot;$add_ntpath\\accounts\\$delete&quot;);

print <<EOF;
<title>Account Deleted</title>
The account <b>$delete</b> has been completely deleted.<br>
<META HTTP-EQUIV=REFRESH CONTENT=&quot;1; URL=admin.pl?pass=$pass&quot;>
<a href=&quot;admin.pl?pass=$pass&quot;>Back to main...</a>
EOF

exit;
}
 
Not sure what the problem is but you could try this to find out.

Set up a logfile somewhere and have your script open it for write each time it runs. Modify your rmdir lines to be something like:-

rmdir(&quot;$add_ntpath\\accounts\\$delete&quot;) or print LOG &quot;rmdir failed because: $!&quot;;

$! should be some sort of descriptive error code.

Note, rmdir will only delete an empty directory, @files is set up from files passed by grep /.log/ which means files not matching .log will not be deleted.

I would be inclined to make @files = readdir so that all files get deleted (including '.' and '..')

paulf



 
Thanks for the reply....

I tried replacing what you said with this:

opendir (DIR, &quot;$add_ntpath\\accounts\\$delete\\logs&quot;);
@files = grep { /.log/ } readdir(DIR);
close (DIR);
foreach $files(@files) { unlink(&quot;$add_ntpath\\accounts\\$delete\\logs\\$files&quot;); }
rmdir(&quot;$add_ntpath\\accounts\\$delete\\logs&quot;);
rmdir(&quot;$add_ntpath\\accounts\\$delete&quot;);

I am not advanced enough to set up the log that you mentioned. It is wierd though, if i manually delete the &quot;logs&quot; directory from the folder represented by &quot;$delete&quot; (the directory name of the user being deleted), and then try the delete function IT WORKS...

It seems the problem is with deleting the &quot;logs&quot; directory. Everything else is deleted okay and the &quot;logs&quot; directory appears to be empty to me. DOes that help any?

Thanks,
john
 
Sorry if I confused you with the logfile thing, it's really very simple. It just means open file where you can write stuff from your script. Then you can read them with in Notepad or whatever when your script has run to see what happened:-

Add this line to the top of your deleteuser subroutine:-

open (LOG, &quot;>>$add_ntpath\\accounts\\log.txt&quot;);

This will create a file called log.txt in your accounts directory which you can print any messages you like into to tell you what's happening in your script. $! is a perl special variable where error messages are sent to by some perl functions such as rmdir. If rmdir fails $! should be set with some kind of explanation why.

So the following line will print the message to the file log.txt with whatever error message is in $!

rmdir(&quot;$add_ntpath\\accounts\\$delete&quot;) or print LOG &quot;rmdir failed to delete $add_ntpath\\accounts\\$delete because: $!\n&quot;;

At the end of your routine don't forget to close the logfile:-

close(LOG);

Hope this helps,

paulf


 
Thanks...i think we're getting somewhere! I did what you said and got this in the log.

rmdir failed to delete d:\websites\swiftfindcom\smartsearch\accounts\j because: Permission denied
rmdir failed to delete d:\websites\swiftfindcom\smartsearch\accounts\j because: Directory not empty

I put the code you gave me after the script attempts to delete &quot;logs&quot; and then after it attempt to delete the entire &quot;$delete&quot; directory. Obviously it can't delete the main directory b/c logs wasn't successfully deleted. But why would i get &quot;permission denied&quot;. The directory is dynamically generated by the signup script when someone signs up. I had tried to change the permissions on the &quot;logs&quot; directory to see if that was the problem and it didn't work then either. Do you have any other wisdom to share? Thanks for all the help so far.

Thanks
John
 
Well, I guess the permission thing is because the script that creates the directory is not leaving it in a state where it can be deleted by another script. In your file creation script after the point where it creates the directory try adding the line, (and create a log file while you're at it):-

chmod (0666, &quot;$new_directory_path&quot;)or print LOG &quot;chmod failed because : $!&quot;;

0666 is read/write permission and 0444 is read only (that's about all the choices you get with windows), also, to delete a directory you need read/write permission for the directory that contains it. I've no experience with windows servers so I can't really offer much more help with the permissions thing.

paulf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top