手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp编程>列表

FSO的几个应用函数

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

  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.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!