Welcome to my ASP Code Website

Deleting a File with ASP



ASP can do file operations, so you can allow users to add, edit and delete files using a web browser. Here is how you would delete a file.

First, you need to get a map to the actual file on the server's hard drive. Let's say you let your users load photos onto your site for some reason. You would let them choose the actual photo from a list on a webpage, and then construct the full path from that name they chose.

PhotoFile = "/photos/" & PhotoURL
PhotoPath = Server.MapPath(PhotoFile)

Now that you have a full path to that file, it's time to give the deletion command. Always make sure you check that a file exists before trying to delete it.

dim ServerFSO
Set ServerFSO=Server.CreateObject("Scripting.FileSystemObject")
if ServerFSO.FileExists(PhotoPath) then
ServerFSO.DeleteFile(PhotoPath)
end if
set ServerFSO=nothing

That's it! The file is now deleted from your server. Of course, make sure you somehow ensure that only people with proper access are in those webpages, deleting things :)

ASP File Function List