Delete file from server problem
I am having problems deleting a file from the server, this is the code I am using:
<%
' CDV - delete file from filesystem prior to standard Delete Record Behaviour
If (CStr(Request("MM_delete")) <> "" And CStr(Request("MM_recordId")) <> "") Then
Dim fs, srcFldr, srcFile, srcThumb
Set fs = CreateObject("Scripting.FileSystemObject")
' strip initial 'x' from xtype to get 'files' folder
'srcFile = Server.MapPath("../files/" & Mid(xtype, 2) & "/" & Request.Form("MTL_docName"))
srcFile = Server.MapPath("../files/" & Mid(xtype, 2) & "/" & Request.Form("MTL_docName"))
If fs.FileExists(srcFile) Then fs.DeleteFile srcThumb, true
If xtype="ximages" Then
srcThumb = Replace(srcFile,".jpg","_small.jpg")
If fs.FileExists(srcThumb) Then fs.DeleteFile srcThumb, true
End If
Set fs = Nothing
End If
%>
Notes:
xtype (for example) = "xdocuments"
No error messages, just that the file is not being deleted. Any ideas?
Thanks.
[1120 byte] By [
Wildcatbob] at [2007-11-11 8:33:51]

# 1 Re: Delete file from server problem
Any ideas anyone?
# 2 Re: Delete file from server problem
I've been playing around with the code, and replaced the dynamic data with "fixed" data but still no joy: :SICK:
The test code is:
<%
If (CStr(Request("MM_delete")) <> "" And CStr(Request("MM_recordId")) <> "") Then
Dim fs, srcFile
Set fs = CreateObject("Scripting.FileSystemObject")
srcFile = Server.MapPath("D:\Domains\something.co.uk\wwwroot\wip\files\documents\somefile.txt")
If fs.FileExists("D:\Domains\something.co.uk\wwwroot\wip\files\documents\somefile.txt") Then fs.DeleteFile srcFile, false
End If
Set fs = Nothing
End If
%>
PS. For some reason, in the code above it is showing a space in the word "documents". Tried fixing it but no luck.
# 3 Re: Delete file from server problem
You don't have On Error Resume Next in your ASP page do you?
# 4 Re: Delete file from server problem
I've added it, but still not working (still no error to help in debugging!):
<%
If (CStr(Request("MM_delete")) <> "" And CStr(Request("MM_recordId")) <> "") Then
On Error Resume Next
Dim fs, srcFldr, srcFile, srcThumb
Set fs = CreateObject("Scripting.FileSystemObject")
srcFile = Server.MapPath("D:\Domains\domain.co.uk\wwwroot\wip\files\documents\somefile.txt")
If fs.FileExists("D:\Domains\domain.co.uk\wwwroot\wip\files\documents\somefile.txt") Then fs.DeleteFile srcFile, false
Set fs = Nothing
End If
%>
# 5 Re: Delete file from server problem
Actually I just wanted to see if that was preventing a run time error in the page.
If you add On Error Resume Next you will need to check the Err object (Err.Number or Err. Description).
I would first verify that your If statement is evaluating to True. Instead of deleting a file try just dumping something out to the page using Response.Write.
# 6 Re: Delete file from server problem
The page is part of a CMS, where most of the data on the page is dynamically generated. I have added several debug response.writes to the page to see the server path (comes up as "/wip/cms/xdocs-delete.asp") and other information - which all work fine.
I just don't see why this code isn't working - is it because it is dependent on another condition taking place first (i.e. "If (CStr(Request("MM_delete")) <> "" And CStr(Request("MM_recordId")) <> "") Then") - and if that is failing then the code is not being executed? That might explain the absense of any errors at all.
Maybe use another method for capturing the form button being submitted? Any suggestions?
# 7 Re: Delete file from server problem
I kinda hit the nail on the head - I moved the code to another location in the page, and sure enough it works fine! :)
Scary how much time one can waste because of such simple mistakes!
Thanks for the help.
# 8 Re: Delete file from server problem
Ok, the above code is working, I can delete a specific, hard-coded file - but the filename needs to be dynamically generated.
If I use a fixed Server.MapPath string, like this...
Server.MapPath("/wip/files/documents/somefile.txt")
...the file is deleted.
The problem now is the file name is dynamic, depending on the document/database entry selected from a list.
PART1: This is the code I am using to create the Server.MapPath string (it creates the correct syntax for the mappath string):
<%
Dim srcDocument, srcDoc
srcDoc = rcsList.Fields.Item("xdocument").Value
srcDocument = "Server.MapPath" & "(""/wip/files/documents/" & srcDoc & """" & ")"
%>
PART2: Then this code to delete the file:
<%
If (CStr(Request("MM_delete")) <> "" And CStr(Request("MM_recordId")) <> "") Then
Dim fs, srcFldr, srcFile, srcThumb
Set fs = CreateObject("Scripting.FileSystemObject")
srcFile = srcDocument
If fs.FileExists(srcFile) Then fs.DeleteFile srcFile, false
Set fs = Nothing
End If
%>
So my question is, how can I make the mappath string work without using a fixed document name?
Thanks in advance.
# 9 Re: Delete file from server problem
Isn't that what your code is already doing? It looks like you're pulling it from a list of some sort. (Recordset possibly?)
# 10 Re: Delete file from server problem
I'm not sure I understand - if I use a fixed (in the code) filename for deletion it works (like "somefile.txt"), but if I make the server.mappath string dynamic (i.e. use the variable "srcDocument") it doesn't delete the file, even though it is a legitimate string.
Have a look at the code again, and you will see what I mean.
Ta.
# 11 Re: Delete file from server problem
Sounds kind of fishy to me. If the file is present it should attempt to delete it. There shouldn't be any difference between hard-coding the path and building it on-the-fly.
I would look back at the If statements again to make certain that they are evaluating to True. Better yet, add an Else statement for each If and display a message when the If condition fails.
# 12 Re: Delete file from server problem
That's what I can't figure out, it should be working. :confused:
Good idea, I'll add the Else statements and see what happens.
# 13 Re: Delete file from server problem
BTW, remove the On Error Resume Next statement if you're not checking the Err object after performing your file operations. Otherwise the code will continue to execute on it's merry way regardless of how many exceptions are occurring.