|
FSO的几个应用函数 |
Author:咖啡虫 PublishTime:2005-7-11 |
1,读取文件中所有字符的函数
Function FSOFileRead(filename) filename = Server.mappath(filename) Set fso = Server.CreateObject("Scripting.FileSystemObject") if not fso.fileExists(filename) then exit function Set cnrs = fso.OpenTextFile(filename,1) While Not cnrs.AtEndOfStream rsline = cnrs.ReadLine rsline = server.HTMLEncode(rsline) Response.Write(rsline & "<br>") Wend end Function
其实就是通过ReadLine(读取行),通过 While Not cnrs.AtEndOfStream 的条件进行循环读取行,来达到读取文件中所有字符。当然也可以使用ReadAll代替多个ReadLine,但主要缺点是将格式进行换行等问题需要再次解决。
引用函数 call FSOFileRead("xxx文件") 即可
2,读取文件中某一行中所有字符的函数
Function FSOlinedit(filename,lineNum) if linenum < 1 then exit function filename = Server.mappath(filename) set fso = Server.CreateObject("Scripting.FileSystemObject") if not fso.fileExists(filename) then exit function set f = fso.opentextfile(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 Response.Write(FSOlinedit) end Function
这次即使用了readall方法,通过split函数将读取的内容以换行为条件,进行数组的定义,提取 lineNum-1(数组从0记数) 所对应的数组值即为 读取的该行值 ,也就是该行中所有的字符了。
函数的调用 call FSOlinedit("xxx文件",35) 表示显示xxx文件的第35行内容
3,读取文件中最后一行内容的函数
Function FSOlastline(filename) filename = Server.mappath(filename) set fso = Server.CreateObject("Scripting.FileSystemObject") if not fso.fileExists(filename) then exit function set f = fso.opentextfile(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 Response.Write(FSOlastline) end function
其实和读取某一行的函数类似,主要即是 数组的上界ubound值 就是最末的值 ,故为最后一行。函数的引用也很简单。
源内容来自:http://www.buzhizhe.com/artical/viewnews.asp?id=8 |
| | |