cd0281 发表于 2013-2-7 00:50:23

枫叶随风ASP文件操作类(FSO)(转)

'============================================================='作者:枫叶随风'QQ:455948725'E-mail:liukun615@126.com'WebSite:http://www.fengyekun.com'=============================================================class FsoClass    private Fso,SourcePath,DestinationPath,FsoFile,FileType      private sub Class_Initialize()      set Fso=server.CreateObject("Scripting.FileSystemObject")      FileType="inc|txt|text|htm|html|shtm|shtml|xhtml|xml|css|js|asp|asa|php|jsp|dwt"    end sub      private sub Class_Terminate()      set Fso=nothing    end sub      private sub ErrorInfo(ErrorNum)      select case ErrorNum      case 1:Str="FsoClass类错误:没有指定Source参数!"      case 2:Str="FsoClass类错误:没有指定Destination参数!"      case 3:Str="FsoClass类错误:执行文件操作时不支持此文件扩展名!"      case 4:Str="FsoClass类错误:执行文件操作时找不到指定文件!"      case 5:Str="FsoClass类错误:执行文件操作时找不到指定文件夹!"      end select                response.write "<script language=""JavaScript"">alert("""&Str&""")</script>"      response.End()    end sub    public property get Source      Source=SourcePath    end property    public property let Source(FilePath)      SourcePath=FilePath    end property      public property get Destination      Destination=DestinationPath    end property    public property let Destination(FilePath)      DestinationPath=FilePath    end property      '获取文件路径    public property get Path      if Destination<>"" then            Path=left(Destination,instrRev(Destination,"/"))      end if    end property      '获取文件名称    public property get Name      if Destination<>"" then            Name=mid(Destination,instrRev(Destination,"/")+1)      end if    end property      '获取文件扩展名    public property get ExtensionName      if Destination<>"" then            ExtensionName=mid(Destination,instrRev(Destination,".")+1)      end if    end property      '获取文件或文件夹的父文件夹包含文件夹名称的路径    public property get ParentFolderPath      if Destination<>"" then            ParentFolderPath=Fso.GetParentFolderName(server.MapPath(Destination))      end if    end property      '获取文件或文件夹的父文件夹名称    public property get ParentFolderName      if Destination<>"" then            dim TempName            TempName=Fso.GetParentFolderName(server.MapPath(Destination))            ParentFolderName=mid(TempName,instrRev(TempName,"\")+1)      end if    end property      '获取文件大小    public property get Size      if Destination<>"" then            if FileExist(Destination) then            set FsoFile=Fso.GetFile(server.MapPath(Destination))            Size=round(FsoFile.size/1024)            end if      end if    end property    '创建时间    public property get DateCreated      if Destination<>"" then            if FileExist(Destination) then            set FsoFile=Fso.GetFile(server.MapPath(Destination))            DateCreated=FsoFile.DateCreated            end if      end if    end property      '上次修改时间    public property get DateLastModified      if Destination<>"" then            if FileExist(Destination) then            set FsoFile=Fso.GetFile(server.MapPath(Destination))            DateLastModified=FsoFile.DateLastModified            end if      end if    end property      '上次访问时间    public property get DateLastAccessed      if Destination<>"" then            if FileExist(Destination) then            set FsoFile=Fso.GetFile(server.MapPath(Destination))            DateLastAccessed=FsoFile.DateLastAccessed            end if      end if    end property      '检测文件是否存在(仅在类内部使用)    private function FileExist(FilePath)      FileExist=Fso.FileExists(server.MapPath(FilePath))    end function      '检测文件夹是否存在(仅在类内部使用)    private function FolderExist(FilePath)      FolderExist=Fso.FolderExists(server.MapPath(FilePath))    end function                '检测文件是否存在    public function FileExists()      if Destination="" then Call ErrorInfo(2)                FileExists=Fso.FileExists(server.MapPath(Destination))    end function      '检测文件夹是否存在    public function FolderExists()      if Destination="" then Call ErrorInfo(2)                FolderExists=Fso.FolderExists(server.MapPath(Destination))    end function      '复制文件    public function CopyFile()      if Source="" then Call ErrorInfo(1)      if Destination="" then Call ErrorInfo(2)      if FileExist(Source)=false then Call ErrorInfo(4)      if FolderExist(Path)=false then Call ErrorInfo(5)                Fso.CopyFile server.MapPath(Source),server.MapPath(Destination)    end function    '复制文件夹//当原文件夹为空文件夹时,此操作无效    public function CopyFolder()      if Source="" then Call ErrorInfo(1)      if Destination="" then Call ErrorInfo(2)      if FolderExist(Source)=false then Call ErrorInfo(4)      if FolderExist(Path)=false then Call ErrorInfo(5)                Fso.CopyFolder server.MapPath(Source),server.MapPath(Destination)    end function      '删除文件    public function DeleteFile()      if Destination="" then Call ErrorInfo(2)                if FileExist(Destination) then            Fso.DeleteFile server.MapPath(Destination)      end if    end function      '删除文件夹    public function DeleteFolder()      if Destination="" then Call ErrorInfo(2)                if FolderExist(Destination) then            Fso.DeleteFolder server.MapPath(Destination)      end if    end function      '移动文件    public function MoveFile()      if Source="" then Call ErrorInfo(1)      if Destination="" then Call ErrorInfo(2)      if FileExist(Source)=false then Call ErrorInfo(4)      if FolderExist(Path)=false then Call ErrorInfo(5)                if FileExist(Destination) then Call DeleteFile()      Fso.MoveFile server.MapPath(Source),server.MapPath(Destination)    end function      '移动文件夹    public function MoveFolder()      if Source="" then Call ErrorInfo(1)      if Destination="" then Call ErrorInfo(2)      if FolderExist(Source)=false then Call ErrorInfo(4)      if FolderExist(Path)=false then Call ErrorInfo(5)                if FolderExist(Destination) then Call DeleteFolder()      Fso.MoveFolder server.MapPath(Source),server.MapPath(Destination)    end function      '创建文件夹    public function CreateFolder()      if Destination="" then Call ErrorInfo(2)                Fso.CreateFolder(server.MapPath(Destination))    end function      '读取文件内容    public function ReadFile()      if Destination="" then Call ErrorInfo(2)      if FileExist(Destination)=false then Call ErrorInfo(4)                set FsoFile=Fso.OpenTextFile(server.MapPath(Destination))      ReadFile=FsoFile.readall      FsoFile.close    end function      '创建文件    public function CreateFile(Content)      if Destination="" then Call ErrorInfo(2)      if instr(FileType,ExtensionName)=0 then Call ErrorInfo(3)      if FileExist(Path)=false then Call ErrorInfo(5)                set FsoFile=Fso.CreateTextFile(server.MapPath(Destination),true)      FsoFile.write(Content)      FsoFile.close    end function      '修改文件内容    public function ModifyFile(Content)      if Destination="" then Call ErrorInfo(2)      if instr(FileType,ExtensionName)=0 then Call ErrorInfo(3)      if FileExist(Destination)=false then Call ErrorInfo(4)                set FsoFile=Fso.OpenTextFile(server.MapPath(Destination),2,true)      FsoFile.write(Content)      FsoFile.close    end function      '在文件内容末尾追加内容    public function WriteFile(Content)      if Destination="" then Call ErrorInfo(2)      if instr(FileType,ExtensionName)=0 then Call ErrorInfo(3)      if FileExist(Destination)=false then Call ErrorInfo(4)                set FsoFile=Fso.OpenTextFile(server.MapPath(Destination),8,true)      FsoFile.write(Content)      FsoFile.close    end function      '更改文件名称和扩展名    public function ChangeFileName(FileName)      if Destination="" then Call ErrorInfo(2)      if FileExist(Destination)=false then Call ErrorInfo(4)                dim t_Path      t_Path=Destination      Destination=Path&FileName      Source=t_Path      Call MoveFile()    end function      '更改文件夹名称    public function ChangeFolderName(FolderName)      if Destination="" then Call ErrorInfo(2)      if FolderExist(Destination)=false then Call ErrorInfo(4)                dim t_Path      t_Path=Destination      Destination=Path&FolderName      Source=t_Path      Call MoveFolder()    end functionend class

属性:
文件原路径 Source
文件目标路径 Destination
文件路径 Path
文件名称 Name
文件大小(以KB为单位) Size
文件扩展名 ExtensionName
父文件名称 ParentFolderName
父文件路径 ParentFolderPath
文件创建时间 DateCreated
上次访问时间 DateLastAccessed
上次修改时间 DateLastModified

方法:
检测文件是否存在 FileExists()
检测文件夹是否存在 FolderExists()
复制文件 CopyFile()
复制文件夹 CopyFolder()
删除文件 DeleteFile()
删除文件夹 DeleteFolder()
移动文件 MoveFile()
移动文件夹 MoveFolder()
创建文件 CreateFile(Content)
创建文件夹 CreateFolder()
修改文件 ModifyFile(Content)
读取文件 ReadFile()
写入内容(以追加方式) WriteFile(Content)
更改文件名称 ChangeFileName(FileName)
更改文件夹名称 ChangeFolderName(FolderName)

实例:检测文件是否存在dim Fsoset Fso=new FsoClassFso.Destination="/Temp/Temp.txt"if Fso.FileExists() then    response.write "文件存在!"else    response.write "文件不存在!"end ifset Fso=nothing实例:检测文件夹是否存在dim Fsoset Fso=new FsoClassFso.Destination="/Temp/Temp"if Fso.FolderExists() then    response.write "文件夹存在!"else    response.write "文件夹不存在!"end ifset Fso=nothing实例:复制文件dim Fsoset Fso=new FsoClassFso.Source="1.txt"Fso.Destination="2.txt"Fso.CopyFile()set Fso=nothing实例:复制文件夹(如果原文件夹为空,则此操作无效)dim Fsoset Fso=new FsoClassFso.Source="/Temp"Fso.Destination="/1/"Fso.CopyFolder()set Fso=nothing实例:删除文件dim Fsoset Fso=new FsoClassFso.Destination="/Temp/Temp.txt"Fso.DeleteFile()set Fso=nothing实例:删除文件夹dim Fsoset Fso=new FsoClassFso.Destination="/Temp"Fso.DeleteFolder()set Fso=nothing实例:移动文件dim Fsoset Fso=new FsoClassFso.Source="/Temp1/a.txt"Fso.Destination="/Temp2/b.txt"Fso.MoveFile()set Fso=nothing实例:移动文件夹dim Fsoset Fso=new FsoClassFso.Source="/Temp1/a"Fso.Destination="/Temp2/b"Fso.MoveFolder()set Fso=nothing实例:创建文件夹dim Fsoset Fso=new FsoClassFso.Destination="/Temp/1"Fso.CreateFolder()set Fso=nothing实例:创建文件dim Fso,ContentContent="aaa"set Fso=new FsoClassFso.Destination="/Temp/1.txt"Fso.CreateFile(Content)set Fso=nothing实例:读取文件内容dim Fso,Contentset Fso=new FsoClassFso.Destination="/Temp/1.txt"Content=Fso.ReadFile()set Fso=nothingresponse.write Content实例:修改文件内容dim Fso,ContentContent="aaa"set Fso=new FsoClassFso.Destination="/Temp/1.txt"Fso.ModifyFile(Content)set Fso=nothing实例:写入内容(以追加方式)dim Fso,ContentContent="aaa"set Fso=new FsoClassFso.Destination="/Temp/1.txt"Fso.WriteFile(Content)set Fso=nothing实例:更改文件名称dim Fsoset Fso=new FsoClassFso.Destination="/Temp/a.txt"Fso.ChangeFileName("b.txt")set Fso=nothing实例:更改文件夹名称dim Fsoset Fso=new FsoClassFso.Destination="/Temp/a"Fso.ChangeFolderName("b")set Fso=nothing
页: [1]
查看完整版本: 枫叶随风ASP文件操作类(FSO)(转)