Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete .pdf file on a server through webpage

Status
Not open for further replies.

Feciman

Programmer
Nov 6, 2002
2
US
I need to know how you delete a file, in this case a pdf, from a folder(../articles/pdf_file/file.pdf) when a user submits something on a webpage. I have a form updating the recordset, but the actual file is still there. is there any way to do this? if so, how?

P.S. sorry if this has been asked before, i couldn't find it in a search.

Thanks,
Feciman
 
use the File System Object, as the example shows below...

however, note that the IIS user (or whatever other user your site is running under, but is usually the IUSR_MACHINENAME acount) must have "full-control" permission to the folders/files you want to delete, or you will get an "Access Denied" message.

if you host your own server, this should be easy enough for you to change, otherwise your ISP may need to get involved to make the change...

Code:
Dim oFSO

' virtual path is relative to your site root ("/")
Dim theVirtual
theVirtual = "/articles/pdf_file/file.pdf"

' determine actual path (server-mapped path) for use by FSO
Dim thePhysical
thePhyscial = Server.MapPath(theVirtual)

Set oFSO = Server.CreateObject("Scripting.FileSystemObject")

If NOT oFSO.FileExists(thePath & "\" & theFile) Then
  Response.Write "File does not exist!"
Else
  oFSO.DeleteFile thePath & "\" & theFile
  Response.Write "File was Deleted!"
End If

Set oFSO = Nothing

Good Luck!
 
You will have to use the FileSystemObject.

Set FSO = Server.CreateObject("Scripting.FileSystemObject")
FSO.DeleteFile("yourfilenamehere")
Set FSO = Nothing
 
Funka, your code is great, but i did notice two things... the bold parts should be replaced with thePhysical. one is just a typo, and the others are just minor mistakes, but if anyone else looks at this, they should know.

thePhyscial= Server.MapPath(theVirtual)

If NOT oFSO.FileExists(thePath & "\" & theFile) Then
Response.Write "File does not exist!"
Else
oFSO.DeleteFile thePath & "\" & theFile
Response.Write "File was Deleted!"
End If

 
thanks! i copied the code from an existing project and went through replacing some of the variables to try and make the example easier to read, but apparently wasn't thorough enough. (and as i'm sure you can see, my original code was piecing together the path and filename stored as 2 vars, rather than using a var called "thePhysical" which has these two parts already combined together.) thanks for pointing this out!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top