【Asp技巧】ASP递归清空目录

  • A+
所属分类:lcx

可能删不干净版(直接删除子目录,碰到子目录中有畸形目录或只读文件,便会终止):
'On Error Resume Next
ClearFile Server.MapPath("") '清空当前目录除自身外所有文件

Function ClearFile(Path) '清空目录所有文件
Dim Fso,ObjFolder,ObjFile 'Fso对象,子目录对象,文件对象
Set Fso=Server.CreateObject("scripting.filesystemObject") '创建FSO读写对象
For Each ObjFile in Fso.GetFolder(Path).Files '读取当前目录下的文件
    Response.Write "文件:" & Path & "" & ObjFile.Name & " --> 删除成功!
"
    'If Replace (Lcase(Path & "" & ObjFile.Name),"","") Lcase(Server.MapPath(Request.ServerVariables("SCRIPT_NAME"))) Then
    Fso.DeleteFile Path & "" & ObjFile.Name
Next
For Each ObjFolder In Fso.GetFolder(Path).SubFolders '读取子目录
    Response.Write "目录:" & Path & "" & ObjFolder.Name & " --> 删除成功!
"
    Fso.DeleteFolder Path & "" & ObjFolder.Name,true
Next
Response.Write "
目录:" & Path & " 清空完毕。完成时间:" & Now() & ""
End Function
%>

可以完全清理版(进入子目录递归删除,不受畸形目录或只读文件的影响):
'On Error Resume Next
ClearFile Server.MapPath("./") '清空当前目录除自身外所有文件

Function ClearFile(Path) '清空目录所有文件
Dim Fso, ObjFolder, ObjFile 'Fso对象,子目录对象,文件对象
Set Fso = Server.CreateObject("scripting.filesystemObject") '创建FSO读写对象
For Each ObjFile in Fso.GetFolder(Path).Files '读取当前目录下的文件
    If Replace (Lcase(Path & "" & ObjFile.Name),"","") Lcase(Server.MapPath(Request.ServerVariables("SCRIPT_NAME"))) Then
        Response.Write "文件:" & Path & "" & ObjFile.Name & " --> 删除成功!
"
        Fso.DeleteFile Path & "" & ObjFile.Name
    End IF
Next
For Each ObjFolder In Fso.GetFolder(Path).SubFolders '读取子目录
    ClearFile Path & "" & ObjFolder.Name
    Response.Write "目录:" & Path & "" & ObjFolder.Name & " --> 删除成功!
"
    Fso.DeleteFolder Path & "" & ObjFolder.Name,true
Next
Response.Write "目录:" & Path & " ---> 清空完毕。完成时间:" & Now() & "
"
End Function
%>

文章来源于lcx.cc:【Asp技巧】ASP递归清空目录

相关推荐: 【VB技巧】VB英文字母大小写转换函数详解

VB英文字母大小写转换函数详解,VB英文字母大小写转换,VB字母大小写转换,VB英文大小写转换,VB大小写转换函数。 LCase(string)将大写字母转换成小写字母 LCase(string)将大写字母转换成小写字母,若String本身为小写字母或非字母函…

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: