text's blog

goodtext.org

 

操作文本的精典Function函数ASP+FSO

发布:goodtext,分类:网站设计,2008-7-22 10:23:7

'取文件大小
Function GetFileSize(FileName)
'//功能:取文件大小
'//形参:文件名
'//返回值:成功为文件大小,失败为-1
    Dim f
    If ReportFileStatus(FileName) = 1 Then
        Set f = fso.Getfile(FileName)
        GetFileSize = f.Size
    Else
        GetFileSize = -1
    End if
End Function

'文件删除
Function deleteAFile(filespec)
'//功能:文件删除
'//形参:文件名
'//返回值:成功为1,失败为-1
    If ReportFileStatus(filespec) = 1 Then
        fso.deleteFile(filespec)
        deleteAFile = 1
    Else
    deleteAFile = -1
    End if
End Function


'显示文件列表
Function ShowFileList(folderspec)
'//功能:目录存在时显示此目录下的所有文件
'//形参:目录名
'//返回值:成功为文件列表,失败为-1
    Dim f, f1, fc, s
    If ReportFolderStatus(folderspec) = 1 Then
    Set f = fso.GetFolder(folderspec)
        Set fc = f.Files
        For Each f1 in fc
           s = s & f1.name
           s = s & "|"
        Next
           ShowFileList = s
    Else
        ShowFileList = -1
    End if
End Function


'!!!
'文件复制
Function CopyAFile(SourceFile,DestinationFile)
'//功能:源文件存在时,才能对文件进行复制,目的文件无影响
'//形参:源文件,目的文件
'//返回值:成功为1,失败为-1
    Dim MyFile
    If ReportFileStatus(SourceFile) = 1 Then
        Set MyFile = fso.GetFile(SourceFile)
        MyFile.Copy (DestinationFile)
        CopyAFile = 1
    Else
        CopyAFile = -1
    End if
End Function


'文件移动
'Response.Write MoveAFile("f:\123\4561.exe","f:\123\4562.txt")
Function MoveAFile(SourceFile,DestinationFile)
'//功能:源文件存在时目的文件不存在时才能对文件进行移动
'//形参:源文件,目的文件
'//返回值:成功为1,失败为-1
If ReportFileStatus(SourceFile)=1 And ReportFileStatus(DestinationFileORPath) = -1 Then
    fso.MoveFile SourceFile,DestinationFileORPath
    MoveAFile = 1
Else
    MoveAFile = -1
End if
End Function


'文件是否存在?
'Response.Write ReportFileStatus("G:\soft\delphi\my_pro\代码库.exe")
Function ReportFileStatus(FileName)
'//功能:判断文件是否存在
'//形参:文件名
'//返回值:成功为1,失败为-1
    Dim msg
    msg = -1
    If (fso.FileExists(FileName)) Then
        msg = 1
    Else
        msg = -1
    End If
    ReportFileStatus = msg
End Function


'文件创建日期
'Response.Write ShowDatecreated("G:\soft\delphi\my_pro\代码库.exe")
'Response.Write ShowDatecreated("G:\soft\delphi\my_pro\复件 代码库.exe")
Function ShowDatecreated(filespec)
'//功能:文件创建日期
'//形参:文件名
'//返回值:成功:文件创建日期,失败:-1
Dim f
    If ReportFileStatus(filespec) = 1 Then
        Set f = fso.GetFile(filespec)
        ShowDatecreated = f.Datecreated
    Else
        ShowDatecreated = -1
    End if
End Function


'文件属性
'Response.Write GetAttributes("G:\soft\delphi\my_pro\复件 代码库.exe")
Function GetAttributes(FileName)
'//功能:显示文件属性
'//形参:文件名
'//返回值:成功:文件属性,失败:-1
    Dim f,Str
    If ReportFileStatus(FileName) = 1 Then
        Set f = fso.GetFile(FileName)
        select Case f.attributes
            Case 0 Str="普通文件。没有设置任何属性。 "
            Case 1 Str="只读文件。可读写。 "
            Case 2 Str="隐藏文件。可读写。 "
              Case 4 Str="系统文件。可读写。 "
            Case 16 Str="文件夹或目录。只读。 "
            Case 32 Str="上次备份后已更改的文件。可读写。 "
            Case 1024 Str="链接或快捷方式。只读。 "
            Case 2048 Str=" 压缩文件。只读。"
        End select
        GetAttributes = Str
    Else
        GetAttributes = -1
    End if
End Function


'最后一次访问/最后一次修改时间
'Response.Write ShowFileAccessInfo("G:\soft\delphi\my_pro\复件 代码库.exe")
Function ShowFileAccessInfo(FileName,InfoType)
'//功能:显示文件创建时信息
'//形参:文件名,信息类别
'// 1 -----创建时间
'// 2 -----上次访问时间
'// 3 -----上次修改时间
'// 4 -----文件路径
'// 5 -----文件名称
'// 6 -----文件类型
'// 7 -----文件大小
'// 8 -----父目录
'// 9 -----根目录
'//返回值:成功为文件创建时信息,失败:-1
'//
Dim f, s
    If ReportFileStatus(FileName) = 1 then
    Set f = fso.GetFile(FileName)
       select Case InfoType
        Case 1 s = f.Datecreated '// 1 -----创建时间
        Case 2 s = f.DateLastAccessed '// 2 -----上次访问时间
        Case 3 s = f.DateLastModified '// 3 -----上次修改时间
        Case 4 s = f.Path '// 4 -----文件路径
        Case 5 s = f.Name '// 5 -----文件名称
        Case 6 s = f.Type '// 6 -----文件类型
        Case 7 s = f.Size '// 7 -----文件大小
        Case 8 s = f.ParentFolder '// 8 -----父目录
        Case 9 s = f.RootFolder '// 8 -----根目录
      End select
        ShowFileAccessInfo = s
    ELse
        ShowFileAccessInfo = -1
    End if
End Function


'写文本文件
Function WriteTxtFile(FileName,TextStr,WriteORAppendType)
    Const ForReading = 1, ForWriting = 2 , ForAppending = 8
    Dim f, m
    select Case WriteORAppendType
      Case 1: '文件进行写操作
        Set f = fso.OpenTextFile(FileName, ForWriting, True)
        f.Write TextStr
        f.Close
        If ReportFileStatus(FileName) = 1 then
            WriteTxtFile = 1
        Else
            WriteTxtFile = -1
        End if
      Case 2: '文件末尾进行写操作
        If ReportFileStatus(FileName) = 1 then
            Set f = fso.OpenTextFile(FileName, ForAppending)
            f.Write TextStr
            f.Close
            WriteTxtFile = 1
        Else
            WriteTxtFile = -1
        End if
      End select
End Function


'写文本文件 指定行数
function FSOlinewrite(filename,lineNum,Linecontent)
    if linenum < 1 then exit function
    dim fso,f,temparray,tempCnt
    set fso = server.CreateObject("scripting.filesystemobject")
    if not fso.fileExists(server.mappath(filename)) then exit function
    set f = fso.opentextfile(server.mappath(filename),1)
    if not f.AtEndofStream then
    tempcnt = f.readall
    f.close
    temparray = split(tempcnt,chr(13)&chr(10))
    if lineNum>ubound(temparray)+1 then
    exit function
    else
    temparray(lineNum-1) = lineContent
    end if
    tempcnt = join(temparray,chr(13)&chr(10))
    set f = fso.createtextfile(server.mappath(filename),true)
    f.write tempcnt
    end if
    f.close
    set f = nothing
end function


'读文本文件
Function ReadTxtFile(FileName)
    Const ForReading = 1, ForWriting = 2
    Dim f, m
    If ReportFileStatus(FileName) = 1 then
        Set f = fso.OpenTextFile(FileName, ForReading)
        m = f.ReadLine
        'm = f.ReadAll
        'f.SkipLine
        ReadTxtFile = m
        f.Close
    Else
        ReadTxtFile = -1
    End if
End Function

数据操作函数  (2008-6-25 7:49:46)

几种过滤HTML代码的应用  (2008-4-27 10:33:17)

ASP防注入危险字符代码  (2008-4-10 18:56:43)

GOODTEXT.ORG留言本 V 1.0  (2008-3-30 22:3:32)

留言管理部分代码 留言本制作过程(3)  (2008-3-30 21:5:59)

显示留言部分代码 GOODTEXT.ORG留言本制作过程(2)  (2008-3-30 20:57:14)

数据库结构设计与链接 GOODTEXT.ORG留言本制作过程(1)  (2008-3-27 18:58:58)

CSS样式控制网页背景(背景颜色和颜色图片)  (2008-3-22 12:44:4)

asp分页做成一个函数 可重复调用  (2008-3-9 9:15:50)

用函数关闭Access数据库  (2008-3-8 9:28:33)