PureUpload delete file before delete/update

35783 Users read it.
by Marcellino Bommezijn (November 22, 2001)

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

User Rating (25 votes)
38 reviews available | Read the reviews »
User Level:Intermediate
Server Model:ASP VBScript

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 >>

User Reviews

Total of 38 reviews
Very neat, well done!
Written by student101 on June 6, 2008

Very, Very neat, well done!Thank you for this one. Glad to know there are real coders out there. Cheers

pure delete
Written by feng xiangling on November 7, 2007

i like this

Works one place and doesn't in the other
Written by Phillip Duncan on February 17, 2004

I've got this working on a client server with the same host as mine, yet when I try to run it on my (same fields, different recordset name) is does not delete the images. The recordset is deleted and there are no errors given. The permissions are ...

Only deletes the first record - Please HELP!
Written by Peter Kutschmann on February 16, 2004

First of all, MANY MANY thanks to Mr. Bommezijn for all your tutorials, it has helped me alot and I am sure many other people. Using your tutorial, I have created a delete multiple record page. This works perfectly. The problem is, all associated ...

No luck!
Written by Curtis Mulder on July 18, 2003

I've moved the recordset in question to the top as suggested but still no luck. Below is the error. ADODB.Field (0x800A0BCD)Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current ...

Using with Smart Image Processor
Written by Phillip Duncan on June 19, 2003

I modified the code to work with Smart Image Processor. I needed it to Delete the image and the Thumbnail that was created. I thought I'd post it in case anyone wants to do the same. Dim aspfile, Imagepath1, ImagePath2 ImagePath1 = ...

modify
Written by fabio nuñez on June 1, 2003

If I use it in modify record the old files don't delete??

RE: Error
Written by Marcellino Bommezijn on June 1, 2003

Please take a look at the comment below the one you posted. It discusses the same error as you have and the solution to it.

Follow us on twitter Subscribe to our RSS feed
Activate your free membership today | Login | Currency