Search code examples
file-ioasp-classicfilesystemobject

Cannot delete a file using ASP DeleteFile method


I'm trying to delete an image file whenever a user clicks a link to delete it. I get no errors, and the error number is zero. However, the file is not deleted. I can save the photos file, but not delete them for some reason. Here is my code:

PhotoFile = "\images\networkPartners\" & rs(fieldName)
PhotoPath = Server.MapPath(PhotoFile)
dim ServerFSO
Set ServerFSO=Server.CreateObject("Scripting.FileSystemObject")
if ServerFSO.FileExists(PhotoPath) then
    response.Write(PhotoPath)

    ServerFSO.DeleteFile(PhotoPath)
end if
set ServerFSO=nothing

When the response.write hits, I do get the actual file path. ("C:/web/images", etc.) All the way up to the file name. All capitalization is correct and so forth. Again, no errors at all when I do the "on error resume next" and write out the error number and string.

Calling:

    ServerFSO.DeleteFile(PhotoPath, true)

results in the error:

Microsoft VBScript compilation error '800a0414'

Cannot use parentheses when calling a Sub

/folder/file_edit.asp, line 32

ServerFSO.DeleteFile(PhotoPath, true)
-------------------------------------^

Solution

  • Check if your files are marked with readonly attribute. If so then use

    FileSystemObject.DeleteFile filename, true 
    

    this will force delete a readonly file.