Replies Back to Article
Delete file before deleting record
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
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
Hello Leon,
You can check if a file exists before the delete action. Take a look at the following code.
Happy Coding!
Paul Keur.
' Add FileSystemObject to newFileSystemObject
<%
Function newFileSystemObject()
set newfilesystemobject="Server".CreateObject("Scripting.FileSystemObject")
End Function
%>
' Create a function to check if the file you want to delete exists.
<%
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")
Set File = CreateObject("Scripting.FileSystemObject")
MM_editCmd.ActiveConnection = MM_editConnection
' This is where we delete the file before we delete the record!
ImagePath = Server.MapPath("images\")
ImagePath = ImagePath & "\" & (rsDelete.Fields.Item("filename").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
End If
%>
The file isn't deleteing so what i did is replace
File.DeleteFile(ImagePath) ...with
Response.Write ImagePath
now this just outputs the image path, but when it outputs the path it doesn't specify the file name but just the directory
C:\Inetpub\wwwroot\Admin\EmployeePhotos
Is this right?, I dont understand whats happening, i've done exactly as the instructions said
This is the code i have inserted
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("..\EmployeePhotos\")
ImagePath = ImagePath & "\" & (Recordset1.Fields.Item("Photo").Value)
'File.DeleteFile(ImagePath)
Response.Write ImagePath
...and i've commented out the redirect line so i get to see the output
the Field in the Access DB is called Photo
PLEASE HELP
Hello Mark,
Send me your asp and i'll see what i can do for you okay?
Happy Coding!
I have a record set "rsdelete" and a deleterecord server behavior, I then add the code you have and I get this error "Object_required:_'rsDelete'". The error shows up in the server logs. I have done a response.write and the file path is correct for the file. Thanks for your help..
Giovanni
My problem is with: File.DeleteFile(ImagePath)
My computer seems to stay in some kind of loop and it never finishes executing the script.
Also why are you using imagepath with small letters in the if statement:
If fileExists(imagepath) Then
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("images\")
ImagePath = ImagePath + "\" + RsDestino.Fields.Item("foto").Value
'File.DeleteFile(ImagePath)
'Response.Write ImagePath
' check if file exists and if true delete the file
If fileExists(imagepath) Then
File.DeleteFile(ImagePath)
'Response.Write ImagePath
End if
I am having similar issues with the script. It somehow seems to not be picking up the File Name to be deleted, .... even though the RecordSet contains it from the select.
If I replace the recordset with the filename and extension, ..... it deletes the file. It may have something to do with how the recordset picks up the values from the Detail page (based on parameter ID passed)....
It looks pretty straightforward .... but I still can't get it to work for me :-(
I have found that if you are passing the Primary ID parameter from the Search Results Page to the Delete Page using a single filter (Primary ID) .... the form that sends the actual delete command only contains that single ID string (and not the column of the actual file ID).
This is what I did to get around it....
1). Add <input type="hidden" name="FileLinkID" value="<%= rsDeleteData.Fields.Item("FileLink").Value %>"> to your form as one of the fields (it has already been picked up by the recordset).
2). Use ... FilePath = FilePath & "\" & (Request("FileLinkID")) . This will pick up the hidden field passed via the form and delete the file from the folder as well as the record from the database....
3). Bob's your Uncle .... :-)
Similarly, you can use the (Request("FileLinkID")) in the code provided later in the user posts to check if the file exists.
I don't know the details of how others got the original code to work. They must have passed multiple parameters via URL ... or .... perhaps used "Form" to pass the parameters. I am not sure exactly on how UltraDev passes these from page to page yet :-?
Hope this helps....
I had same problem. Thanks Harry - it worked great!
However, I have a new one. "File not found" I am sure my problem is with my Server.MapPath. (I always semm to have problems with it.)
My code...
ImagePath = Server.MapPath("photos\")
ImagePath = ImagePath & "\" & (Request("FileLinkID"))
The absolute path to my image sub-dir is d:/html/users/domainName/html/photos
The absolute path to my delete file sub-dir is d:/html/users/domainName/html/_private
Why doesn't the above code work?
In your MapPath ......you have:-----------
ImagePath = Server.MapPath("photos\")
ImagePath = ImagePath & "\" & (Request("FileLinkID"))
Shouldn't it be ("\photos") since you are adding the backslash to it in the next line anyways?? and MapPath returns "C:\path\to\directory" ??
This is assuming that the photos directory is located in your root directory where the MapPath will point to.
You could also try hardcoding the path to get to the image directory to test functionality. That depends on whether your ISP moves things around on the server.
I can't write english (i'm a frog) so i think it's your solution :
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("photos\")
ImagePathe = ImagePath & "\" & (Request("FileLinkID"))
File.DeleteFile(ImagePathe)
'Response.Write (ImagePathe)
nice coding
Hello to you all,
I've made an example program for those of you who wants to know how to get the function working. Just email me and i will send it to you.
Please forgive me that i am not able to answer all of your questions right away, i am a very busy guy and i whish i had more time to help you all.
Keep a look out in the extension dir because i am finished with the extension for this and will post it in the beta section for testing first.
Happy Coding!
Paul Keur
Dear Mr. Paul,
I've try to delete file before delete records. There's 2 file field store on my database. how can i delete both of them.
This's my code:
ImagePath = Server.MapPath("images\")
ImagePath = ImagePath & "\" & (Request("FileLinkID"))
File.DeleteFile(ImagePath)
ImagePath = ImagePath & "\" & (Request("FileLinkID1"))
File.DeleteFile(ImagePath)
and on the form tag:
<input type="hidden" name="FileLinkID" value="<%= RsInsert.Fields.Item("SmallImage").Value %>">
<input type="hidden" name="FileLinkID1" value="<%= RsInsert.Fields.Item("BigImage").Value %>">
Thanks
Dear Mr. Paul,
I've try to delete file before delete records. There's 2 file field store on my database. how I can delete both of them.
This's my code:
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("images\")
ImagePath = ImagePath & "\" & (Request("FileLinkID"))
File.DeleteFile(ImagePath)
ImagePath = ImagePath & "\" & (Request("FileLinkID1"))
File.DeleteFile(ImagePath)
and on the form tag:
<input type="hidden" name="FileLinkID" value="<%= RsDelete.Fields.Item("SmallImage").Value %>">
<input type="hidden" name="FileLinkID1" value="<%= RsDelete.Fields.Item("BigImage").Value %>">
Thanks
Try this:
Set File = CreateObject("Scripting.FileSystemObject")
FolderPath = Server.MapPath("images\")
ImagePath = FolderPath & "\" & (Request("FileLinkID"))
File.DeleteFile(ImagePath)
ImagePath = FolderPath & "\" & (Request("FileLinkID1"))
File.DeleteFile(ImagePath)
I think that it does the job, but I have not tried it :-)
this worked for me...
Are you using Norton Anti-Virus on the server? They recently added 'Script
Blocking' (via Live Update), and this causes problems when using
delete/write operations with the FSO. If you are, go to your NAV control
panel and uncheck Script Blocking - you will need to reboot.
HTH
Tom Steeper
http://www.webuality.com/t-cubed/
Hello Tom.
Thank You. That was the problem. I uncheked script protection and in Norton Anti-Virus andthe script worked. Finally I know I'm not such a rock.
You are the man.
Julio
I am trying to construct a piece of code that will run whenever a default page is opened that will update all records stored in a database very reguarly, which takes the strain off of administration. Each record has two images associated with it, thus I wish to delete the images also. This is the code that I am trying to use, but I am receiving an error that says "Object Not Supported".
<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="../Connections/eastalamed.asp" -->
<%
set rsDelete = Server.CreateObject("ADODB.Recordset")
rsDelete.ActiveConnection = MM_eastalamed_STRING
rsDelete.Source = "SELECT * FROM tblStork"
rsDelete.CursorType = 0
rsDelete.CursorLocation = 2
rsDelete.LockType = 3
rsDelete.Open()
rsDelete_numRows = 0
%>
<%
MM_editQuery = " DELETE FROM tblStork WHERE date > (now-332) "
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_eastalamed_String
Set File = CreateObject("Scripting.FileSystemObject")
FolderPath = Server.MapPath("babyphotos/")
ImagePath = FolderPath & "/" & (rsDelete.Fields.Item("picone").Value)
File.DelteFile(ImagePath)
ImagePath = FolderPath & "/" & (rsDelete.Fields.Item("pictwo").Value)
File.DelteFile(ImagePath)
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
%>
i am trying to get this to work on chillisoft and getting the error message back
Microsoft VBScript runtime error '800a01a8'
Object required: 'Recordset1'
/attachupdate.asp, line 91
this corresponds to the line
ImagePath = ImagePath & "\" & (Recordset1.Fields.Item("attachment").Value)
I don't know where i am going wrong, or is it same as alot of things, that chillisoft won't allow it
Waldo,
Your code for multiple file delete works great if I've loaded up 2 images! Would you mind offering the code you wrote, which is below, where it allows the second delete file to execute only if a second file exists? In my admin area, users can load either one or two files - but on the delete page, if they've only loaded one image, it still looks for a second file to delete and then balks because it can't find one.
Thank you so much for your great help!
Sincerely,
Larry B. (lbuzecky@cimanet.com)
Set File = CreateObject("Scripting.FileSystemObject")
FolderPath = Server.MapPath("images\")
ImagePath = FolderPath & "\" & (Request("FileLinkID"))
File.DeleteFile(ImagePath)
ImagePath = FolderPath & "\" & (Request("FileLinkID1"))
File.DeleteFile(ImagePath)
Paul,
I have a situation where I need to delete two images, the first of which always exists, the second of which is an optional upload. I adapted your code for multiple file deletes and checking to see if a file exists and got this:
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("\private\AccidentAdmin")
ImagePath = ImagePath & "\" & (rsArticle.Fields.Item("doc1").Value)
If fileExists(imagepath) Then
File.DeleteFile(ImagePath)
End if
ImagePath = ImagePath & "\" & (rsArticle.Fields.Item("doc2").Value)
If fileExists(imagepath) Then
File.DeleteFile(ImagePath)
End if
Problem is, with this code neither image ends up getting removed. Could you point me in the right direction? I would really appreciate it!
Thank you,
Larry
I know how to delete multiple images
the first thing you must remeber is:
copy this code into your web code
<%
Function newFileSystemObject()
set newfilesystemobject="Server".CreateObject("Scripting.FileSystemObject")
End Function
%>
<%
Function fileExists(aFileSpec)
fileexists="newFileSystemObject".FileExists(aFileSpec)
End Function
%>
ok!
the next thing is.
modify the code like this:
If (Not MM_abortEdit) Then
' execute the delete
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
' This is where we delete the file before we delete the record!
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath1 = Server.MapPath("\ASP\jcgs\IMAGES")
ImagePath = ImagePath1 & "\" & (Recordset1.Fields.Item("tupid").Value)
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If
ImagePath = ImagePath1 & "\" & (Recordset1.Fields.Item("tupid1").Value)
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
look it careful ImagePath1 and ImagePath ,when code File.DeleteFile(ImagePath) Execute ,ImagePath1 doesn't change .so you can use again. the code , I have tested ,it really work!
I come form china,my English is not very well .but I hope can help you! If have any question you can Email to me : hsj178@163.net. good job paul!
I am using PWS on win98 and each time i go to use the demo file upload.asp it will not allow it. The initial page will load but it will below is the error displayed on reload - upload submited.
Anyone know how to correct this problem please email mason_fok@hotmail.com
Server object error 'ASP 0177 : 800401f3'
Server.CreateObject Failed
/DeleteFile/upload.asp, line 135
800401f3
I am using PWS on win98 and each time i go to use the demo file upload.asp it will not allow it. The initial page will load but it will below is the error displayed on reload - upload submited.
Anyone know how to correct this problem please email juan@webmastersolution.com
Server object error 'ASP 0177 : 800401f3'
Server.CreateObject Failed
/DeleteFile/upload.asp, line 135
800401f3
I've been fooling with this too, but there's some error handling that need to be done to avoid getting yourself really cranky.
- you've got to check that the actual physical file exists before deleting it or everything just kind of hangs
- you also got to make sure the path to the file directory exists before you do the Server.MapPath thing - or catch the error tha FSO throws, otherwise more death by another thousand nailbites.
I used the Pure Asp Upload 2.9, to upload the images in a dinamic folder with name of the record ID.
To delete them Ineed to delete also the folder where they are stored. The name of any folder corispond of the Name of the Record ID. So hou to form the Image path to delete not a single image , buth the folder where the images are stored, for the corisponding Record ID ??????
Alexander Nikolov
Hello Mark,
Check your iis if you've got 'ENABLE PARENT PATH' checked.
Sinds you are using 'ImagePath = Server.MapPath("..\fichier")' PARENT PATH must be enabled to get this line of code to work. To check if this is the case goto your iis manager then click on your website followed by right clicking on properties, then click on the tab 'HOME DIRECTORY', then click on the button 'Configuration' and the click on the tab 'APP OPTIONS'.
You can also try ImagePath = Server.MapPath("\fichier").
Happy Coding!
Paul
I used Pure ASP File Upload 2.1.3 on a windows 2000 server. When i try to delete a file a got this error message:
(Sorry for the french but i dont know the message in english)
HTTP 500 - Erreur interne de serveur
Internet Explorer
If i dont write " File.DeleteFile(ImagePath)" the code works correctly.
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("..\fichier")
ImagePath = ImagePath & "\" & (rsDocument.Fields.Item("nomPhysique").Value)
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If
During I useing these codes, I found a problem. If the file's attribute is read_only, system will report you don't have right to delete. So I made some changes to solve this problem.New codes like this:
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("images\")
ImagePath = ImagePath & "\" & (rsDelete.Fields.Item("filename").Value)
'***New codes***
Set Myfile = File.GetFile(ImagePath)
Myfile.Attributes = 0 ''Set file's attribute to normal
'**************
File.DeleteFile(ImagePath)
Hope somebody enjoy it...
Paul, or whovever picks this up
Thank you for this script - It's just what I was looking for. I am a novice at this, but using the scripts in your demo application I have managed to pass a variable table name to the view.asp page through the URL (view.asp?table=blah). In this way the view.asp page returns the records contained in the table chosen by the user.
How can I pass that same variable through to the delete page, replacing the references to the 'upload' table in your script?
I'm totally stuck! Any help you can give would be much appreciated.
Regards
Gerard
I've been working on this all day (!) I hope someone can help me.
I have a list of tables - the user clicks on the tables which passes the table name to the view.asp page in Paul Keur's demo application using the url (view.asp?table=blah_table).
I'm trying to pick up the table name 'blah' in the delete.asp script. How can I replace the two references to the 'upload' table in the delete.asp script with the variable value (blah_table, or whatever) passed into the view page?
Please can someone help?
Many thanks
Passing the table name into the page 'delete.asp' via the url, I changed delete.asp by replacing the references to the table 'upload' with 'Request.QueryString("Table")' Hey Presto! it works.
This would not work yesterday - but this morning it does. the moral?: ... I have no idea what the moral is.
All I need now is a system to return a message when there are no files left in the table to delete.
Can anyone help with that?
Gerard
Can anyone shed some light on how to do this in a stored procedure?
I'm happy with the WHERE IN but not got a clue on how to make my SP delete the file associated with a record as it is stored in a folder and not the database. Server.MapPath isnt accepted by SQL server
Use this if there are any errors, I think.
'just in case an error occurs add this line.
on error resume next
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("../images")
ImagePath = ImagePath & "\" & (rsImages.Fields.Item("filename_img").Value)
' check if file exists and if true delete the file
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If