Delete file before deleting record

This tutorial explains how to write a code that deletes a file that has been uploaded to your server before deleting the record where the image belongs to.
June 18, 2001: Added a demo application.

This tutorial explains how to write a code that deletes a file that has been uploaded to your server before deleting the record where the image belongs to. Download an example application to see how to use this code (keep the directory structure when extracting this zip).

After you've created a delete page look for the following code and add the blue and red lines to it.
What it does is that it first creates a variable for the delete command (blue lines) and second creates a variable for the path to the filename (red lines). I used in this example a directory 'images' where it can find the file in and added the actual filename to it from the database.

Code:

<%
' *** 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\")
      ImagePath = ImagePath & "\" & (rsDelete.Fields.Item("filename").Value)

      File.DeleteFile(ImagePath)
      MM_editCmd.CommandText = MM_editQuery
      MM_editCmd.Execute
      MM_editCmd.ActiveConnection.Close

      If (MM_editRedirectUrl <> "") Then
         Response.Redirect(MM_editRedirectUrl)
      End If
   End If

End If
%>

Note:
Keep in mind that you must open the recordset which holds the filename first!!! In my example Iused a recordset named 'rsDelete' with a fieldname called 'filename' in it.
After this we can go ahead and delete the file and record. That's all there is to it.

Changelog:
June 18, 2001: Added a demo application.

I am working on an extension for this which will work with the 'PureASPUpload' from George Petrov.

Happy Coding!

Paul Keur (www.dreamsites.nl)

Comments

Nice

April 21, 2001 by Leon Radley
Thanks alot, just saved me a lot of hassel, you the man ;P

Thank You

April 21, 2001 by Max Loddo

Paul,

Thanks a lot. Great tip to complete Petrov's ASP FileUpload. Now uploading and managing files on the server have become easy tasks.

Keep up the good work!

Max

deleting multipe images

April 26, 2001 by Leon Radley
how would i do to delete multiple images?

RE: deleting multipe images

April 26, 2001 by Paul Keur

Hello Leon,

Using the sample code:

  Set File = CreateObject("Scripting.FileSystemObject")
      
ImagePath = Server.MapPath("images\")
      ImagePath = ImagePath & "\" & (rsDelete.Fields.Item("filename").Value)

      File.DeleteFile(ImagePath)

You could do a simple basic solution:

  Set File = CreateObject("Scripting.FileSystemObject")
      
ImagePath = Server.MapPath("images\")
      ImagePath = ImagePath & "\" & (rsDelete.Fields.Item("filename1").Value)

      File.DeleteFile(ImagePath)

      ImagePath = ImagePath & "\" & (rsDelete.Fields.Item("filename2").Value)
      File.DeleteFile(ImagePath)

Or you can use a 'FOR NEXT' loop where you add a variable to the filename which is a much nicer solution.

I am making an extension for this which will work with the 'PureAspUpload' from George Petrov. One of the features will be the option to select between single or multiple deletion.

Happy Coding!

Paul Keur

See all 45 Comments

You must me logged in to write a comment.