Thursday, March 13, 2008

How to Delete a file from Server (ASP Classic / ASP.NET)

If you are using ASP Classic to delete a file from server you can use the following script:
<%
Dim filepath, strPhysicalPath
filepath = Session("filetodelete")
strPhysicalPath = Server.MapPath("directoryvirtualpath" & filepath)
set oFile = CreateObject("Scripting.FileSystemObject")
oFile.DeleteFile (strPhysicalPath), True
set oFile = nothing
%>

but if you are using ASP.Net You will need the Following Code:
Dim oDir As New DirectoryInfo(path)
Dim oFile As FileInfo
For Each oFile In oDir.GetFiles(SearchPattern)
oFile.Delete()
Next

No comments: