Forums

ASP

This topic is locked

Delete File From Server with Server.MapPath

Posted 20 May 2007 07:19:23
1
has voted
20 May 2007 07:19:23 Mike Leslie posted:
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

Replies

Replied 20 May 2007 07:28:07
20 May 2007 07:28:07 Mike Leslie replied:
("C:\Inetpub\wwwroot\HBCC\CallCenter\EMPLOYEE_FILES\"& filepath)
this is the correct path in the first code block, i copied th ewrong one, this is correct,
how do i get the second example to work right
Replied 20 May 2007 20:26:04
20 May 2007 20:26:04 Mike Leslie replied:
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
%>

Reply to this topic