PureUpload delete file before delete/update

This tutorial explains how to delete a file before the update or delete sql statement is executed.

This tutorial will explain how to delete a file while using an update form or a delete form in conjunction with PureASPUpload 1 & 2.

The article of Paul Keur also explains this, but i had some questions from people which couldn't get the picture with all the code in the demo pages.

It's actually very simple.

First of all you will need to these two functions to make it work.

<%
Function newFileSystemObject()
set newFileSystemObject=Server.CreateObject("Scripting.FileSystemObject")
End Function
%>


<%
Function fileExists(aFileSpec)
fileExists=newFileSystemObject.fileExists(aFileSpec)
End Function
%>

Find your code for either the delete or update sql statement that starts with:

Delete statement:

<%
' *** Delete Record: construct a sql delete statement and execute it

Update Statement:

<%
' *** Update Record: construct a sql delete statement and execute it

Just above these sql statements place the two functions as mentioned above.

Then insert the code as shown below in the update or delete statement:

' execute the delete/update
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection

' This is where we delete the file before we delete the record!
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("..\upload\")
ImagePath = ImagePath & "\" & (rsNews.Fields.Item("image").Value)

' check if file exists and if true delete the file
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If

MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close

As you see it also will work for an update ! This is very handy when using it for example a news administration with images, where it is allowed to edit/change images. This way all images will be deleted from your system/server.

One important note is that the database field that is called from the recordset, in this example (rsNews.Fields.Item("image").Value), needs to be opened before you call this database field. So, if your recordset is below the sql statement for update or delete, move it to the very top.

Example code of the recordset:

<%
Dim rsNews__MMColParam
rsNews__MMColParam = "1"
if (Request.QueryString("ID") <> "") then rsNews__MMColParam = Request.QueryString("ID")
%>
<%
set
rsNews = Server.CreateObject("ADODB.Recordset")
rsNews.ActiveConnection = MM_connUDZONE_STRING
rsNews.Source = "SELECT * FROM tblNews WHERE ID = " + Replace(rsNews__MMColParam, "'", "''") + ""
rsNews.CursorType = 0
rsNews.CursorLocation = 2
rsNews.LockType = 3
rsNews.Open()
rsNews_numRows = 0
%>

The below example would delete multiple files, uploaded with PureASPUpload.

Creating a dynamic array and using the statement For Each..Next to get access to this array.

Setting up the array with the fields of the database that holds the filename of the file uploaded:

<%
' *** Delete Record: construct a sql delete statement and execute it
Dim aspfile, Imagepath1, ImagePath2, ImagePath3, ImagePath4

ImagePath1 = Server.MapPath("upload\")
ImagePath1 = ImagePath1 & "\" & (Recordset1.Fields.Item("file1").Value)

ImagePath2 = Server.MapPath("upload\")
ImagePath2 = ImagePath2 & "\" & (Recordset1.Fields.Item("file2").Value)

ImagePath3 = Server.MapPath("upload\")
ImagePath3 = ImagePath3 & "\" & (Recordset1.Fields.Item("file3").Value)

ImagePath4 = Server.MapPath("upload\")
ImagePath4 = ImagePath4 & "\" & (Recordset1.Fields.Item("file4").Value)

Dim ImageLibrary(3)
ImageLibrary(0) = ImagePath1
ImageLibrary(1) = ImagePath2
ImageLibrary(2) = Imagepath3
ImageLibrary(3) = ImagePath4

Using the For Each..Next statement to access the array and set the delete action:

If (Not MM_abortEdit) Then
' execute the delete
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection

' setup the statement

For Each aspfile In ImageLibrary
Set File = CreateObject("Scripting.FileSystemObject")
' check if file exists and if true delete the file
If fileExists(aspfile) Then
File.DeleteFile(aspfile)
End If
Next


MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close

Hope this answers the requests for some people, for deleting multiple files.

Note: the ending slash has to be in to make it work in the Server.MapPath. For some reason it works with me without this slash (?)
ImagePath = Server.MapPath("..\upload\")

 

 

 

 

Marcellino Bommezijn

Marcellino BommezijnMarcellino Bommezijn is one of the managers at dmxzone.com. He is a contributor on the tutorials section.

Owner of Senzes Media (http://www.activecontent.nl) which provides professional services and web applications for mid-sized companies.

ActiveContent CMS is the ASP.NET Content Management solution that is used for building professional and rich-featured websites.

See All Postings From Marcellino Bommezijn >>

Comments

Needing help

November 15, 2001 by Adam Toohey
Hi, it seems there is more to what you say that's it!!  I am sure that I am doing something wrong.  I suppose I don't really see exactly what you added to the recordset.

Is this where the location of the file being deleted is supposed to be? ImagePath = Server.MapPath("..\upload")

I followed your tutorial exactly.  I didn't add anything to the recordset.  Do I need to?  All I did with the recordset was move it to the top.

After following the tutorial this is the error I am getting.


Error Type:
ADODB.Fields (0x800A0CC1)
Item cannot be found in the collection corresponding to the requested name or ordinal.
/wilkinsons/admin/adminhouseland/delete.asp, line 94

Line 94 is this code ImagePath = ImagePath & "\" & (deletehomes.Fields.Item("image").Value)  deletehomes is the name of the recordset.

Any ideas?

Thanks in advance,

Adam Toohey

RE: Needing help

November 15, 2001 by Adam Toohey
I changed ImagePath = ImagePath & "\" & (deletehomes.Fields.Item("image").Value) to ImagePath = ImagePath & "\" & (deletehomes.Fields.Item("thumbnailpic").Value)  which is the name of the pic I am trying to delete and am now not getting any errors.  However, the file is not being deleted.

Any ideas?

I have been trying to get this working for a long time now and would really appreciate some help getting this working.

Looking forward to a response.

Adam Toohey 

RE: Needing help

November 15, 2001 by Marcellino Bommezijn

Adam,

This is the code that you actually add in the update/delete statement:

' This is where we delete the file before we delete the record!
Set File = CreateObject("Scripting.FileSystemObject")

ImagePath = Server.MapPath("..\upload")
ImagePath = ImagePath & "\" & (rsNews.Fields.Item("image").Value)

' check if file exists and if true delete the file
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If

Of course you have to rename your own recordset and corresponding database fields. The database field "image" is just an example. But you already solved that one.

Now you only have to determine what the relative path is to your directory where the image/file resides. The path ("..\upload") is also an example of what it could be. So depending on your location and name of that directory in your root in could be different. For example ("..\..\images") or ("..\files"). 

Marcellino

RE: RE: Needing help

November 15, 2001 by Adam Toohey

Hi Marcellino,

I have done all of this and still the file isn't being deleted.  I am sure there is something easy that I haven't done.  This is the code I have included in my page..... 
<%
Function newFileSystemObject()
set newfilesystemobject="Server".CreateObject("Scripting.FileSystemObject")
End Function
%>
<%
Function fileExists(aFileSpec)
fileExists=newFileSystemObject.FileExists(aFileSpec)
End Function
%>
<%
' *** Delete Record: construct a sql delete statement and execute it

If (CStr(Request("MM_delete")) <> "" And CStr(Request("MM_recordId")) <> "") Then

  ' create the sql delete statement
  MM_editQuery = "delete from " & MM_editTable & " where " & MM_editColumn & " = " & MM_recordId

  If (Not MM_abortEdit) Then
    ' execute the delete
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection
' This is where we delete the file before we delete the record!
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("..\..\images\jpgs\houseland")
ImagePath = ImagePath & "\" & (deletehomes.Fields.Item("thumbnailpic").Value)
' check if file exists and if true delete the file
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
    If (MM_editRedirectUrl <> "") Then
      Response.Redirect(MM_editRedirectUrl)
    End If
  End If

Usually i would put a / after putting the destination in example ../../images/jpgs/houseland/  How come there is no / after the destination in your code.  I will also send you the entire page.  Maybe you can find time to have a look.

Thanks,

Adam

See all 38 Comments

You must me logged in to write a comment.