呵呵,FSO文件操作类,类几乎包含所有的FSO操作..
代码:
<% ''=============================================== ''@Date 2006-6-8 ''@Author NetFog ''@ModifyTime 2006-6-8 ''@Desc 文件操作类 ''=============================================== Class Cls_FSO
Private ObjFSO 'FSO对象 Private IsError '是否出错
''=========================================== ''@name Class_Initialize ''@desc 初始化 ''=========================================== Private Sub Class_Initialize() IsError = False End Sub
''=========================================== ''@name Errs ''@desc 返回布尔值 ''=========================================== Public Property Get Errs() Errs = IsError End Property
''=========================================== ''@name OpenFSO ''@desc 创建FSO对象 ''=========================================== Private Sub OpenFSO() Set ObjFSO = Server.CreateObject("Scripting.FileSystemObject") End Sub
''=========================================== ''@name CloseFSO ''@desc 注销FSO对象 ''=========================================== Private Sub CloseFSO() Set ObjFSO = Nothing End Sub
''=========================================== ''@name Read ''@desc 读取文件内容,成功则返回内容,失败则返回False ''=========================================== Public Function Read(ByVal vFilePath) On Error Resume Next Dim File,tmpContent OpenFSO If ObjFSO.FileExists(Server.MapPath(vFilePath)) = False Then Read = False : Read = False : IsError = True : Exit Function Set File = ObjFSO.OpenTextFile(Server.MapPath(vFilePath),1,False) tmp = File.ReadAll File.Close Set File = Nothing CloseFSO If Err.Number = 0 Then Read = tmp Else Read = False IsError = True End if End Function
''=========================================== ''@name ExistsFile ''@desc 文件是否存在,存在为True,不存在为False ''=========================================== Public Function ExistsFile(ByVal vFilePath) On Error Resume Next Dim ThisBool OpenFSO If ObjFSO.FileExists(Server.MapPath(vFilePath)) Then ThisBool = True Else ThisBool = False End If CloseFSO If Err.Number = 0 Then ExistsFile = ThisBool Else ExistsFile = False IsError = True End If End Function
''=========================================== ''@name ExistsFolder ''@desc 文件夹是否则在,存在则返回True,否则返回False ''=========================================== Public Function ExistsFolder(ByVal vFolderPath) 'On Error Resume Next Dim ThisBool OpenFSO If ObjFSO.FolderExists(Server.MapPath(vFolderPath)) Then ThisBool = True Else ThisBool = False End If CloseFSO If Err.Number = 0 Then ExistsFolder = ThisBool Else EsistsFolder = False IsError = True End If End Function
''=========================================== ''@name SetFile ''@desc 重置文件内容/生成新文件,成功为True,失败为False ''=========================================== Public Function SetFile(ByVal vFilePath,ByVal vContent) On Error Resume Next Dim WF OpenFSO If ObjFSO.FileExists(Server.MapPath(vFilePath)) Then ObjFSO.DeleteFile(Server.MapPath(vFilePath)) End If Set WF = ObjFSO.CreateTextFile(Server.MapPath(vFilePath), True) WF.Write vContent WF.Close Set WF = Nothing CloseFSO If Err.Number = 0 Then SetFile = True Else SetFile = False IsError = True End if End Function
''=========================================== ''@name DelFile ''@desc 删除文件,成功为True,失败为False ''=========================================== Public Function DelFile(ByVal vFilePath) On Error Resume Next OpenFSO If ObjFSO.FileExists(Server.MapPath(vFilePath)) Then ObjFSO.DeleteFile(Server.MapPath(vFilePath)) End If CloseFSO If Err.Number = 0 Then DelFile = True Else DelFile = False End if End Function
''=========================================== ''@name CopyFile ''@desc 复制文件并返回布尔值,成则为True,失败为False,如果源文件不存在,返回False,目标文件存在则覆盖。 ''=========================================== Public Function CopyFile(ByVal vSrsFilePath,ByVal vDsnFilePath) On Error Resume Next OpenFSO If ObjFSO.FileExists(Server.MapPath(vSrsFilePath)) = False Then CopyFile = False IsError = True CloseFSO Exit Function Else ObjFSO.CopyFile Server.MapPath(vSrsFilePath),Server.MapPath(vDsnFilePath),1 End If CloseFSO If Err.Number = 0 Then CopyFile = True Else CopyFile = False IsError = True End If End Function
''=========================================== ''@name MoveFile ''@desc 移动文件并返回布尔值,成则为True,失败为False.如果源文件不存在,返回False。目标文件存在则覆盖 ''=========================================== Public Function MoveFile(ByVal vSrsFilePath,ByVal vDsnFilePath) On Error Resume Next OpenFSO If ObjFSO.FileExists(Server.MapPath(vSrsFilePath)) = False Then MoveFile = False IsError = True CloseFSO Exit Function Else If ObjFSO.FileExists(Server.MapPath(vDsnFilePath)) Then ObjFSO.DeleteFile(Server.MapPath(vDsnFilePath)) ObjFSO.MoveFile Server.MapPath(vSrsFilePath),Server.MapPath(vDsnFilePath) End If CloseFSO If Err.Number = 0 Then MoveFile = True Else MoveFile = False IsError = True End If End Function
''=========================================== ''@name NewFolder ''@desc 创建文件夹,存在则不操作并返回False,成功返回True,失败返回False ''=========================================== Public Function NewFolder(ByVal vFolderPath) On Error Resume Next OpenFSO If ObjFSO.FolderExists(Server.MapPath(vFolderPath)) Then NewFolder = False : IsError = True : Exit Function ObjFSO.CreateFolder(Server.MapPath(vFolderPath)) CloseFSO If Err.Number = 0 Then NewFolder = True Else NewFolder = False IsError = True End If End Function
''=========================================== ''@name DelFolder ''@desc 删除文件夹,不存在则返回False,存在则删除并返回True ''=========================================== Public Function DelFolder(ByVal vFolderPath) On Error Resume Next OpenFSO If ObjFSO.FolderExists(Server.MapPath(vFolderPath)) = False Then DelFolder = False : IsError = True : Exit Function ObjFSO.DeleteFolder(Server.MapPath(vFolderPath)) CloseFSO If Err.Number = 0 Then DelFolder = True Else DelFolder = False IsError = True End If End Function
''=========================================== ''@name CopyFolder ''@desc 复制文件夹,不存在则返回False,目标文件夹存在则覆盖 ''=========================================== Public Function CopyFolder(ByVal vSrsFolderPath,ByVal vDsnFolderPath) On Error Resume Next OpenFSO If ObjFSO.FolderExists(Server.MapPath(vSrsFolderPath)) = False Then CopyFolder = False : IsError = True : Exit Function ObjFSO.CopyFolder Server.MapPath(vSrsFolderPath),Server.MapPath(vDsnFolderPath),True CloseFSO If Err.Number = 0 Then CopyFolder = True Else CopyFolder = False IsError = True End If End Function
''=========================================== ''@name MoveFolder ''@desc 移动文件夹,源文件夹不存在则返回False,目标文件夹存在则覆盖 ''=========================================== Public Function MoveFolder(ByVal vSrsFolderPath,ByVal vDsnFolderPath) On Error Resume Next OpenFSO If ObjFSO.FolderExists(Server.MapPath(vSrsFolderPath)) = False Then MoveFolder = False : IsError = True : Exit Function If ObjFSO.FolderExists(Server.MapPath(vDsnFolderPath)) Then ObjFSO.DeleteFolder(Server.MapPath(vDsnFolderPath)) ObjFSO.MoveFolder Server.MapPath(vSrsFolderPath),Server.MapPath(vDsnFolderPath) CloseFSO If Err.Number = 0 Then MoveFolder = True Else MoveFolder = False IsError = True End If End Function
''=========================================== ''@name FileInfo ''@desc 取得文件信息,文件不存在则返回False ''=========================================== Public Function FileInfo(ByVal vFilePath,ByVal vType) On Error Resume Next Dim File,tmpInfo vType = Ucase(vType) OpenFSO If ObjFSO.FileExists(Server.MapPath(vFilePath)) = False Then FileInfo = False : IsError = True : Exit Function Set File = ObjFSO.GetFile(Server.MapPath(vFilePath)) Select Case vType Case "SIZE" tmpInfo = File.Size Case "NAME" tmpInfo = File.Name Case "TYPE" tmpInfo = File.Type Case "PATH" tmpInfo = File.Path Case "PROPERTY" tmpInfo = File.Attributes Case "LASTUSETIME" tmpInfo = File.DateLastAccessed Case "LASTMDYTIME" tmpInfo = File.DateLastModified Case "CREATETIME" tmpInfo = File.DateCreated Case "PARENTFOLDER" tmpInfo = File.ParentFolder Case "DRIVE" tmpInfo = File.Drive Case Else tmpInfo = "" End Select Set File = Nothing CloseFSO If Err.Number = 0 Then FileInfo = tmpInfo Else FileInfo = False IsError = True End If End Function
''=========================================== ''@name FolderInfo ''@desc 取得文件夹信息,文件夹不存在则返回False ''=========================================== Public Function FolderInfo(ByVal vFolderPath,vType) On Error Resume Next Dim Folder,tmpInfo vType = Ucase(vType) OpenFSO If ObjFSO.FolderExists(Server.MapPath(vFolderPath)) = False Then FolderInfo = False : IsError = True : Exit Function Set Folder = ObjFSO.GetFolder(Server.MapPath(vFolderPath)) Select Case vType Case "SIZE" tmpInfo = Folder.Size Case "NAME" tmpInfo = Folder.Name Case "ISROOTFOLDER" tmpInfo = Folder.IsRootFolder Case "PARENTFOLDER" tmpInfo = Folder.ParentFolder Case Else tmpInfo = "" End Select Set Folder = Nothing CloseFSO If Err.Number = 0 Then FolderInfo = tmpInfo Else FolderInfo = False IsError = True End If End Function
''=========================================== ''@name FilesInFolder ''@desc 取得文件中所有的文件名列表存入数组中 ''=========================================== Public Function FilesInFolder(ByVal vFolderPath) On Error Resume Next Dim tmpArr(),n,item,FilesCollection n = 0 OpenFSO If ObjFSO.FolderExists(Server.MapPath(vFolderPath)) = False Then FilesInFolder = False : IsError = True : Exit Function Set FilesCollection = ObjFSO.GetFolder(Server.MapPath(vFolderPath)) Redim tmpArr(FilesCollection.Files.Count-1) For each item in FilesCollection.Files tmpArr(n) = item n = n+1 Next Set FilesCollection = Nothing CloseFSO If Err.Number =0 then FilesInFolder = tmpArr Else FilesInFolder = False IsError = True End if End Function
''=========================================== ''@name SubFolder ''@desc 取得文件中所有的文件名列表存入数组中 ''=========================================== Public Function SubFolder(ByVal vFolderPath) On Error Resume Next Dim tmpArr(),n,item,FolderCollection n = 0 OpenFSO If ObjFSO.FolderExists(Server.MapPath(vFolderPath)) = False Then SubFolder = False : IsError = True : Exit Function Set FolderCollection = ObjFSO.GetFolder(Server.MapPath(vFolderPath)) Redim tmpArr(FolderCollection.SubFolders.Count-1) For each item in FolderCollection.SubFolders tmpArr(n) = item n = n+1 Next Set FolderCollection = Nothing CloseFSO If Err.Number =0 then SubFolder = tmpArr Else SubFolder = False IsError = True End if End Function
End Class %>
说明文档 ''=============================================== ''@Date 2006-6-8 ''@Author NetFog ''@ModifyTime 2006-6-8 ''@Desc 文件操作类 ''===============================================
类 名:Cls_FSO
简 介:将文件操作简单化,包括文件内容读取、文件/文件夹删除、文件/文件夹移动、文件/文件夹复制, 同时包含了取得文件/文件夹的相关信息的方法
注意事项:在所有的示例中FSO.Errs返回True表示有错误发生,如果返回False表示无错误,如果使用请见示例
可进行的操作有:复制文件/复制文件夹/删除文件/删除文件夹/文件是否存在/移动文件 创建新文件夹/读取文件内容/重置文件内容/生成新文件/移动文件夹 取得文件相关信息/取得文件夹中的文件列表/取得文件夹相关信息 取得文件夹中的子文件夹列表/检测文件夹是否存在
其中的常用操作有:文件是否存在/创建新文件夹/读取文件内容/重置文件内容/生成新文件/检测文件夹是否存在
[此贴被 NetFog(q70213526) 在 07月10日14时28分 编辑过]
|