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

deleting selected files using asp

Status
Not open for further replies.

eljacobs

Programmer
Oct 13, 2003
47
GB
Hi

I have a site that displays images along with details of the images from a database.

I'd like to be able to select a number of these images and delete the details of the image from the database as well as the actual images from the server.

So far i've managed to delete the records from the database using a delete command but the script i have used to delete the image itself does not seem to be working.

The script does not throw an error but the page simply does not load.

The code is below.

Thanks in advance.

Regards

Elliot
---------------------------------------------------------

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../Connections/con_imglib.asp" -->

'delete records from db
<%
if Request.QueryString("delete") <> "" then
if(Request.QueryString("delete") <> "") then cmd_delete__var_imageref = Request.QueryString("delete")

%>

<%

set cmd_delete = Server.CreateObject("ADODB.Command")
cmd_delete.ActiveConnection = MM_con_imglib_STRING
cmd_delete.CommandText = "DELETE FROM tbl_images WHERE image_ref IN ('" + Replace(cmd_delete__var_imageref, "'", "''") + "')"
cmd_delete.CommandType = 1
cmd_delete.CommandTimeout = 0
cmd_delete.Prepared = true
cmd_delete.Execute()
End If

%>

'delete file

<%Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

objFSO.DeleteFile "C:\InetPub\ Image Library\images\library\thumbs\"&Request.QueryString("delete")&".jpg", False

Set objFSO = Nothing%>

'redirect back to the gallery page
<%

Response.Redirect("admin_gallery.asp?cat="&Request.QueryString("cat"))

%>
 
first thing I would do is put the comment "'redirect page back to the gallery" inside your scripting tags or it is going to display

try this

Code:
objFSO.DeleteFile ("C:\InetPub\[URL unfurl="true"]wwwroot\Sahney[/URL] Image Library\images\library\thumbs\" & Request.QueryString("delete") & ".jpg")
If that doesn't fix it, I would then add this above the line
Code:
response.write(string is -C:\InetPub\[URL unfurl="true"]wwwroot\Sahney[/URL] Image Library\images\library\thumbs\" & Request.QueryString("delete") & ".jpg")
response.end
to see exactly what the string you are trying to work with is.

Steve Davis
hey.you AT HaHaHa.com.au

Me? I can't even spell ASP!

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination
 
Thanks Steve

I tried the changes you suggested and I got the following error.

Error Type:
Microsoft VBScript compilation (0x800A03EE)
Expected ')'
/sahney image library/ line 38, column 27
response.write(string is -C:\InetPub\ Image Library\images\library\thumbs\" & Request.QueryString("delete") & ".jpg")
--------------------------^

Any ideas?
 
Ok, I'm betting the problem is that the delete field is holding multiple file names. Since it is holding multiple filenames your going to need to break it down into individual strings (maybe pslit into an array) before concatenating ontot the file path. Otherwise you would be telling the FSO object to delete a file called:
C:\InetPub\ Image Library\images\library\thumbs\File1, File2, File3, etc.jpg

Which is going to determine that the file already doesn't exist, so no delete takes place.

The Response.Redurect can stay, just get rid of all the places where you leave the ASP code blocks because those are beinmg sent to the client browser, once you send something tothe client-browser you can't redirect. So close up the code like so:
Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../Connections/con_imglib.asp" -->

'delete records from db
<%
if Request.QueryString("delete") <> "" then
if(Request.QueryString("delete") <> "") then cmd_delete__var_imageref = Request.QueryString("delete")

set cmd_delete = Server.CreateObject("ADODB.Command")
cmd_delete.ActiveConnection = MM_con_imglib_STRING
cmd_delete.CommandText = "DELETE FROM tbl_images  WHERE image_ref IN ('" + Replace(cmd_delete__var_imageref, "'", "''") + "')"
cmd_delete.CommandType = 1
cmd_delete.CommandTimeout = 0
cmd_delete.Prepared = true
cmd_delete.Execute()
End If

'delete file
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

[highlight]Replace this section with the suggestions from above
objFSO.DeleteFile "C:\InetPub\[URL unfurl="true"]wwwroot\Sahney[/URL] Image Library\images\library\thumbs\"&Request.QueryString("delete")&".jpg", False[/highlight]

Set objFSO = Nothing

'redirect back to the gallery page
Response.Redirect("admin_gallery.asp?cat="&Request.QueryString("cat"))

%>

Between the suggestions at the top and the code blocks joined, you should be good to go,

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
Help, the rampaging, spear-waving, rabid network gnomes are after me!
 
I think there is a " missing in this line:

response.write(string is -C:\InetPub\ Image Library\images\library\thumbs\" & Request.QueryString("delete") & ".jpg")
response.end

where should it go?
 
In the beginnig of your reponse.write:
response.write([highlight]"[/highlight]string is -C:\InetPub\ Image Library\images\library\thumbs\" & Request.QueryString("delete") & ".jpg")

Thart output should show you the comma delimited values I was concerned about in the top of my previous post.

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
Help, the rampaging, spear-waving, rabid network gnomes are after me!
 
the code didn't delete the file and didn't redirect back to the gallery page. Instead it just diplayed the following text...

string is -C:\InetPub\ Image Library\images\library\thumbs\2.jpg

E
 
sorry about the crappy code, but hopefully when asking a question people in these forums know enough themselves to see that what is offered as an answer is a guide and may contain a syntactical error and they can use what we offer as a bassi for their own fix.

My idea was that it displays the string you are using to try to delete the file. In this case "C:\InetPub\ Image Library\images\library\thumbs\2.jpg"

Does that match the file name? If so, step one is good. If not, there is a problem with the way you are building the string.

Now take out the response.end and see if it actually deletes the file - it seems you don't realise that that is an effective break line in the code. It stops it running at that point, so no, it won't do the actual delete or redirection. That was the whole point.

Steve Davis
hey.you AT HaHaHa.com.au

Me? I can't even spell ASP!

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination
 
Not, you may want to comment out the Redirect temporarily, that Response.Write will cause it to fail,

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
Help, the rampaging, spear-waving, rabid network gnomes are after me!
 
My apologies. I didn't realise what you were attempting to do with that code.

You will have to excuse my ignorance as i am quite new to this so sometimes it is difficult to see what each section of code is meant to do.

Ok, i'm quite sure the string is correct now so there must be another problem. Do you think it could be something to do with the permissions of the folder that holds the images?

I'm still not getting any error messages so it is hard to determine what the problem is. The page begins to load then crashes and that's about as far as i get!

Thanks again for your help.

Elliot
 
is it on the web so we can try it?

Steve Davis
hey.you AT HaHaHa.com.au

Me? I can't even spell ASP!

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination
 
When you say the page crashes, does it ouput an error message or what?

It's possible the message your getting would be the key to finding a solution, if you could dupilcate it and paste it here that would be helpful.

Permissions is generally a good guess in situations like this. If your not using IIS authentication for your web server (by default IIS doesn't use it for IIS5+ - 2000/XP/2003) then IIS executes scripts as a default user - IUSR_MachineName I think.

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
Help, the rampaging, spear-waving, rabid network gnomes are after me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top