FSO 读写文本文件 几种情形

上一篇 / 下一篇  2011-08-26 16:30:49 / 个人分类:杂粹

使用FSO修改文件特定内容的函数
Function FSOChange(FileName,Target,String)
   Dim objFSO,objCountFile,FiletempData
   Set bjFSO = Server.CreateObject("Scripting.FileSystemObject")
   Set bjCountFile = objFSO.OpenTextFile(Server.MapPath(filename),1,True)
   FiletempData = objCountFile.ReadAll
   objCountFile.Close
   FiletempData = Replace(FiletempData,Target,String)
   Set bjCountFile = objFSO.CreateTextFile(Server.MapPath(filename),True)
   objCountFile.Write FiletempData 
   objCountFile.Close
   Set bjCountFile=Nothing
   Set bjFSO = Nothing
End Function

使用FSO读取文件内容的函数
Function FSOFileRead(filename)
   Dim objFSO,objCountFile,FiletempData
   Set bjFSO = Server.CreateObject("Scripting.FileSystemObject")
   Set bjCountFile = objFSO.OpenTextFile(Server.MapPath(filename),1,True)
   FSOFileRead = objCountFile.ReadAll
   objCountFile.Close
   Set bjCountFile = Nothing
   Set bjFSO = Nothing
End Function

使用FSO读取文件某一行的函数
Function FSOlinedit(filename,lineNum)
   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
     Set f = nothing
     temparray = split(tempcnt,chr(13)&chr(10))
     If lineNum > ubound(temparray) + 1 then
       exit function
     else
       FSOlinedit = temparray(lineNum-1)
     end if
   end if
end function

使用FSO写文件某一行的函数
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

使用FSO添加文件新行的函数
Function FSOappline(filename,Linecontent)
   dim fso,f
   set fso = server.CreateObject("scripting.filesystemobject")
   if not fso.fileExists(server.mappath(filename)) then exit function
   set f = fso.opentextfile(server.mappath(filename),8,1)
   f.write chr(13)&chr(10)&Linecontent
   f.close
   set f = nothing
End Function

读文件最后一行的函数
Function FSOlastline(filename)
   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
     set f = nothing
     temparray = split(tempcnt,chr(13)&chr(10))
     FSOlastline = temparray(ubound(temparray))
   End If
End Function
'============================================== 
'读取指定目录下的文件函数 
'Call LoadFile(目录路径) 
'============================================== 

Sub LoadFile(FilePath) 
 Dim FSO,FObj,FileSign 
 On Error Resume Next 
 FilePath = Server.MapPath(FilePath) 
 Set FSO = CreateObject("Scripting.FileSystemObject") 
 Set FObj = FSO.GetFolder(FilePath) 
 Response.Write "<table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""3"">" 
 Response.Write "<tr><td>文件名称</td><td>文件大小</td></tr>"
 FileSign = "<span style=""color:#FF9900"">◆</span> " 
 For Each Files In FObj.Files 
  Response.Write "<tr><td>" & FileSign & Files.Name & "</td><td>" & Round(Files.Size/1024) & " KB" & "</td></tr>"
 Next 
 Response.Write "</table>"
End Sub

TAG: FSO VBA 读写文件

 

评分:0

我来说两句

日历

« 2024-05-02  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 4286
  • 日志数: 5
  • 建立时间: 2011-08-26
  • 更新时间: 2011-12-22

RSS订阅

Open Toolbar