Skip to main content
Inspiring
May 20, 2007
Answered

ASP Delete File from server

  • May 20, 2007
  • 2 replies
  • 374 views
I have made a file sharing app, and the description and file name are stored in my access db, when i delete the recordset for the file i also want to delete the actual file. i am able to delete the file with the full path entered and the file name as a variable,
with this code:

<%
Dim filepath
filepath = Session("file_del")
set filedel = CreateObject("Scripting.FileSystemObject")
filedel.DeleteFile("C:\localhost\CallCenter\EMPLOYEE_FILES\"& filepath), True
set filedel = nothing
%>

I put the name of the file in the file_del session variable on the page that deletes the recordset.
But i would like to make it work on my hosted site without having to change the path to my sites, so i have made this code:

<%
Dim filepath
filepath = Session("file_del")
strPhysicalPath = Server.MapPath(filepath)
set filedel = CreateObject("Scripting.FileSystemObject")
filedel.DeleteFile strPhysicalPath , True 'Line that causes ERROR
set filedel = nothing
%>

But the error comes File Not found.
if i just display the strPhysicalPath variable on the page without the delete action it displays the proper path.
how can i get the strPhysicalPath to be read, i have tried all of the ways i can think of

filedel.DeleteFile strPhysicalPath , True
filedel.DeleteFile & strPhysicalPath & , True
filedel.DeleteFile (strPhysicalPath) , True
filedel.DeleteFile ("" & strPhysicalPath & "") , True
filedel.DeleteFile ( & strPhysicalPath) , True

But none of them work ?
Any help would be great
This topic has been closed for replies.
Correct answer MikeL7
After some rest and a closer examination, i realized the error was in the mappath line, it wasnt including the folder that held the files. strPhysicalPath = Server.MapPath(filepath) was wrong so, I added the folder name: strPhysicalPath = Server.MapPath("EMPLOYEE_FILES/" & filepath) then the file is deleted. so complete code is:

<%
Dim filepath, strPhysicalPath
filepath = Session("file_del")
strPhysicalPath = Server.MapPath("EMPLOYEE_FILES/" & filepath)
set filedel = CreateObject("Scripting.FileSystemObject")
filedel.DeleteFile (strPhysicalPath), True
set filedel = nothing
%>

2 replies

MikeL7AuthorCorrect answer
Inspiring
May 20, 2007
After some rest and a closer examination, i realized the error was in the mappath line, it wasnt including the folder that held the files. strPhysicalPath = Server.MapPath(filepath) was wrong so, I added the folder name: strPhysicalPath = Server.MapPath("EMPLOYEE_FILES/" & filepath) then the file is deleted. so complete code is:

<%
Dim filepath, strPhysicalPath
filepath = Session("file_del")
strPhysicalPath = Server.MapPath("EMPLOYEE_FILES/" & filepath)
set filedel = CreateObject("Scripting.FileSystemObject")
filedel.DeleteFile (strPhysicalPath), True
set filedel = nothing
%>
MikeL7Author
Inspiring
May 20, 2007
("C:\Inetpub\wwwroot\CallCenter\EMPLOYEE_FILES\"& filepath)
This is the path physical path, i copied the wrong one in the first example