在使用asp来删除文件时,我一般直接使用下面的语句直接删除文件,但是往往这个文件在其他地方被删除了,那么这里文件不存在,就会报错.
哪位大侠能给出一个完善、详细的删除文件的代码?
FiLePaTh = Server.MapPath(".")& "" & path& "" &file '路径和文件名
Set fso = CreateObject("Scripting.FileSystemObject") 'FSO对象
fso.DeleteFile(filepath) '删除
Set fso = nothing '关闭FSO对象
---------------------------------------------------------------
on error resume next
---------------------------------------------------------------
function deletefiles(path)
'on error resume next
Set fs1=Server.CreateObject("Scripting.FileSystemObject")
if fs1.FileExists (path) then
fs1.DeleteFile path,True
else
response.write "文件不存在!"
end if
Set fs1=nothing
end function
---------------------------------------------------------------
加入一下代码即可!
If (fso.FileExists(filespec)) Then
fso.DeleteFile(filepath)
End If
---------------------------------------------------------------
同上
---------------------------------------------------------------
Function Generic_FileDeletion(byval strpath,byval intShowLabel)
' ERROR CHECKING!!!!...
IF strpath = "" or isnull(strpath) then
Response.Write("Sorry but a path is required when calling this function")
Response.End
End IF
' ERROR CHECKING!!!!...
IF intShowLabel = "" or isnull(intShowLabel) then
Response.Write("Sorry but the paramter
1<b>intShowLabel</b>
is missing. PARAMETER IS REQUIRED")
Response.End
End IF
Filename = Server.MapPath(strpath)
Set fs = CreateObject("s cripting.FileSystemObject")
If fs.FileExists(filename) Then
fs.DeleteFile(filename)
if intShowLabel = 1 then
response.write "File" & "
1<b>(" & strpath & ")</b>
" & " has Been Deleted with Success.
1<br/>
"
end if
Else
response.write "No path was found, or file does not exist to delete...
1<br/>
"
End If
End Function