·您的位置: 首页 » 资源教程 » 编程开发 » ASP » FSO的几个应用函数

FSO的几个应用函数

类别: ASP教程  评论数:0 总得分:0
 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值 就是最末的值 ,故为最后一行。函数的引用也很简单。
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1