FSO中除了可以对驱动器、文件夹的操作以外,功能最强大的就是对文件的操作了。它可以用来记数、内容管理、搜索还可生成动态HTML页面等等。
一、fso.OpenTextFile 无需多说,fso.OpenTextFile就是打开某个文件了,一般情况之下是打开的txt文本文件。所以首先我们先建立一个txt文件,然后通过FSO来读取其中的内容。
1,info.txt
name:cnbruce sex:male
建立了该文件,下面再做个ASP页面,当然最好两个文件是在同一目录下。
2,opentxt.asp
<% whichfile=server.mappath("info.txt") Set fso = CreateObject("Scripting.FileSystemObject") Set txt = fso.OpenTextFile(whichfile,1) rline = txt.ReadLine rline = rline & "<br>" & txt.ReadLine Response.Write rline txt.Close %>
需要注意:无论是通过FSO打开驱动器、打开文件夹、打开文件,以及以后要接触到的打开数据库,都只能是打开绝对物理路径地址。但一般情况是上传到空间服务商那,不能很直接地了解到自己文件的所在位置,所以强烈推荐使用server.mappath方法:平台移植性强,适用性强。
CreateObject("Scripting.FileSystemObject")建立了FSO组件的连接,fso.OpenTextFile(whichfile,1)打开了info.txt该文件。参数“1”表示“ForReading:以只读方式打开文件。不能写这个文件。”,其他还有参数“2”表示“ForWriting:以写方式打开文件”,参数“8”表示“ForAppending:打开文件并从文件末尾开始写”。
打开了该文件,接下来是不是要显示文件中的内容?那就通过txt.ReadLine方法读取文本中的一整行,如果需要继续读取下一行,则继续使用txt.ReadLine方法。当然初此还有其它的读取方法,比如txt.Read(7)读取指定数量的字符,txt.ReadAll返回文本中的全部内容。
二、fso.CreateTextFile 如fso.CreateFolder建立文件夹般,fso.CreateTextFile则是建立文件了。
3,creattxt.asp
<% whichfile=server.mappath("info.txt") Set fso = CreateObject("Scripting.FileSystemObject") Set MyFile = fso.CreateTextFile(whichfile,True) MyFile.WriteLine("My Name Is CN-Bruce") MyFile.WriteLine("My Sex Is Male") MyFile.Close %> <a href="opentxt.asp">查看内容</a>
本次建立的文件是上一info.txt文件,fso.CreateTextFile(whichfile,True)其中的参数true即表示能覆盖已有文件。建立后需要向里面添加数据就采用“MyFile.WriteLine”了。
那现在就可以建立一个简单的文本记数器了,还记得以前的记数?:1,通过application、session、global.asa进行记数;2,通过Counter组件进行记数。但两者都有通病,就是不能保存,如果服务器重新启动后,是不是所有记数全部清空了呢:)那现在就可以使用文本来记录数据了,即使重启,下次提取的也还是该文件。
试验:文本计数器
首先建立一记数的文本文件counter.txt,设定初始值为“1”
4,counter.txt
1
接着是记数的ASP文件,功能是显示文本的记数,本做加1的记数,然后还要将新的记数写入文本文件。
5,txtcount.asp
<% whichfile=server.mappath("counter.txt") '打开文件并将其值读取,最后关闭连接释放资源 set fso=createobject("Scripting.FileSystemObject") set openfile=fso.opentextfile(whichfile,1) visitors=openfile.readline openfile.close '页面显示记数内容并做加1运算 response.write "您是本页的第"&visitors&"位访客" visitors=visitors+1 '将新的数值添加写入到文本,最后关闭所有连接释放资源 set creatfile=fso.createtextfile(whichfile) creatfile.writeLine(visitors) creatfile.close set fso=nothing %>
那根据这个可以继续地扩展内容:比如让记数用数字图片显示。当然前提就是你需要0-9的10张记数图片,并将此图片放于img文件夹中。 下为一增强的txtcount.asp内容代码
<% whichfile=server.mappath("counter.txt")
set fso=createobject("Scripting.FileSystemObject") set openfile=fso.opentextfile(whichfile,1) visitors=openfile.readline openfile.close CountLen=len(visitors) response.write "您是本页的第"
for i=1 to 6-countLen '表示最大值999999 response.write "<img src="http://www.21kn.com/Files/BeyondPic/2007-7/8/077800435871803.gif"></img>" next response.write "位访客"
visitors=visitors+1 set creatfile=fso.createtextfile(whichfile) creatfile.writeLine(visitors) creatfile.close set fso=nothing %>
本程序中采用的是mid函数,该函数的作用是返回某字符串中从第几位字符开始的几个字符。格式即为:Mid(string,start,length) <script language=vbs> cn_string= "cnbruce love cnrose" cn_start = 9 cn_length = 4 alert (mid(cn_string,cn_start,cn_length)) </script> 学会了FSO提取文件值,也学会了将信息输入到文件中,那下面就再来应用应用下。
不知道你有没有这样的习惯:看到一个文件,不自觉的右键选择用记事本打开。呵呵,几乎没有哪个文件是不可以的。所以现在,可以默认所有文件都是文本,只是后缀名不同而已;那么也就是说,现在可以提取任一文件的内容信息。OK,就来想象一下:
1,提取一个文件的路径(采用file按钮进行查找定位) 2,将该路径文件打开,并读取所有行 3,显示读取的信息
一、viewcode.asp
<% Function ShowCode(filename) Set fso = Server.CreateObject("Scripting.FileSystemObject") Set cnrs = fso.OpenTextFile(filename, 1) While Not cnrs.AtEndOfStream rsline = cnrs.ReadLine rsline = server.HTMLEncode(rsline) Response.Write(rsline & "<br>") Wend end Function %>
<form action="viewcode.asp" method="post"> 输入文件名<input type="file" name="filename"> <input type="submit" value="查看源程序"> </form>
<% file=request.form("filename") response.write (file & "源程序如下<hr>") If trim(file)<> "" then Call ShowCode(file) End If %>
以上程序调试时,可以选择html,asp页面,也可以打开任一应用程序等。
定义的ShowCode函数,主要作用是打开、读取并显示文件中所有信息内容。注意添加了server.HTMLEncode(rsline),针对含有标准HTML代码的文件。
显示文件中所有行即用一条件循环进行遍历显示了。 While Not cnrs.AtEndOfStream ... Wend
接着,下面的这个例题具体就涉及open方法的问题了,还记得?正常情况之下打开文件是采用fso.OpenTextFile("c:\testfile.txt",1),参数1的作用是:以只读模式打开文件。不能对此文件进行写操作。如果现在已经存在一文件,需要进行追加写入,则该怎么办呢?简单,参数为8即可。
PS:这里还有一种读取的方法。
<% whichfile=server.mappath("test.txt") Set fso = CreateObject("Scripting.FileSystemObject") Set txt = fso.OpenTextFile(whichfile,1) rline = txt.ReadAll rline=replace(Server.HtmlEncode(rline),Chr(13),"<br>") Response.Write rline txt.Close %>
这有什么用呢?呵呵,亚玛逊的网络故事接龙就是如此:能接龙就需要首先要显示原有故事,然后自己添加故事写入文件。这其中的写入文件最讲究的就是追加写入了。所以下面就可以实现。
二、story.asp
<% If not request.Form("NextLine")="" then Set fso=Server.CreateObject("Scripting.FileSystemobject") textfile1=Server.MapPath("story.txt") set cnrs=fso.OpenTextFile(textfile1,8) cnrs.WriteLine(Request.Form("NextLine")) cnrs.Close end if %> 故事如下: <% Set fso=Server.CreateObject("Scripting.FileSystemObject") textfile1=Server.MapPath("story.txt") set cnrs=fso.OpenTextFile(textfile1,1) while not cnrs.AtEndOfStream Response.Write " " & cnrs.ReadLine wend cnrs.close %> <hr> <form method="post" action="story.asp"> 请输入这个故事的新行:<input name="NextLine" type="text" size="70"> <input type="submit" value="提交"> </form>
整个就是一很简单的读取信息和加入信息的混合利用,相信有了前面的基础看懂应该不成问题。当然还缺少个story.txt文件,里面写好故事开头就可以了。
再下面,继续来,该侧重点主要就是练习一些函数的使用技巧了。
1,instr函数:返回某字符串在另一字符串中第一次出现的位置。 比如现在查找字母“A”在字符串“A110B121C119D1861”中第一次出现的位置,则可以
<script language=vbs> my_string = "A110B121C119D1861" a_num = instr(my_string,"A") alert(a_num) </script> 同样字母“B”的位置也就能确定。现在就来最关键的:提前字母“A”和“B”中间的值“110”。还记得mid函数吗?mid函数的主要作用是:从字符串中返回指定数目的字符。比如现在的“110”则应该是从字符串的第2位取得3个单位的值。 <script language=vbs> my_string = "A110B121C119D1861" a_value = mid(my_string,2,3) alert(a_value) </script> 但设想一下:如果不是“110”,而是“1100”,那是不是要提取4位……这样就显出程序的不够完美。 所以继续思考:所提取的值,永远是在字母“A”后面的,且值也永远是在字母“A”和“B”之间的,那么只要分别提取出“A”、“B”的位置,则中间数值的起始位应是字母“A”位+1,中间数值的长度应是字母“B”位-字母“A”位-1那么现在就可以让程序完美起来: <script language=vbs> my_string = "A110B121C119D1861" a_num = instr(my_string,"A") b_num = instr(my_string,"B") a_value = mid(my_string,a_num+1,b_num-a_num-1) alert(a_value) </script> OK,那么现在你也就完全可以把字母“B”、“C”、“D”后面的值一一提取了。 当然需要注意的就是“D”后面有几位怎么取呢?采用字符串总长度-字母D所在位置数就可以了。 <script language=vbs> my_string = "A110B121C119D1861" a_num = instr(my_string,"A") b_num = instr(my_string,"B") c_num = instr(my_string,"C") d_num = instr(my_string,"D") total_num = len(my_string) a_value = mid(my_string,a_num+1,b_num-a_num-1) b_value = mid(my_string,b_num+1,c_num-b_num-1) c_value = mid(my_string,c_num+1,d_num-c_num-1) d_value = mid(my_string,d_num+1,total_num-d_num) alert(a_value) alert(b_value) alert(c_value) alert(d_value) </script> 用到现在,你也许确实学到了不少,但也许会提出疑问:这个放在FSO文件操作里有什么作用呢? 那下面才是我们的正题:用FSO进行简单的文本投票。
投票页面首要的就是显示各类项目的投票数,并相应赋于某个变量。然后判断本次投票的选相,相对应地将投票数值加1,完毕后再将所有值继续写入文本。
1,一个HTML表单页website.html 以做投票点击的平台。 <form action="result.asp" method="post"> <input type="radio" name="website" value="A" checked> cnbruce.com<br> <input type="radio" name="website" value="B"> blueidea.com<br> <input type="radio" name="website" value="C"> it365cn.com<br> <input type="radio" name="website" value="D"> 5d.cn<br> <input type="submit"> <input type="reset"> 2,接受表单页值的result.asp <% whichfile=server.mappath("site.txt") set fso=createobject("Scripting.FileSystemObject") set thisfile=fso.opentextfile(whichfile) my_string=thisfile.readline
a_num = instr(my_string,"A") b_num = instr(my_string,"B") c_num = instr(my_string,"C") d_num = instr(my_string,"D") total_num = len(my_string)
a_value = mid(my_string,a_num+1,b_num-a_num-1) b_value = mid(my_string,b_num+1,c_num-b_num-1) c_value = mid(my_string,c_num+1,d_num-c_num-1) d_value = mid(my_string,d_num+1,total_num-d_num)
select case request.form("website") case "A": a_value=a_value+1 case "B": b_value=b_value+1 case "C": c_value=c_value+1 case "D": d_value=d_value+1 end select
mynew_string="A" & cstr(a_value) & "B" & cstr(b_value) & "C" & cstr(c_value) & "D" & cstr(d_value) set newfile=fso.createtextfile(whichfile) newfile.writeLine(mynew_string) newfile.close set fso=nothing %> 当前投票:<br> cnbruce.com:<%=a_value%><br> blueidea.com:<%=b_value%><br> it356cn.com:<%=c_value%><br> 5d.cn:<%=d_value%><br> <a href="website.html">返回继续</a>
有了上面函数的基础,看这个应该不是很难的
3,最后不要忘了的记数文件site.txt
格式:A1B1C1D1
OK,三个文件就可以胜任一个很简单的投票系统了,如果要加强,需要细化的则结合以前的知识吧,比如投过一次后设置session或者cookies,当再次投票时候判断如果session或者cookies存在则不允许,也就是简单的投票防作假手段了……当然更多的还是要自己去想去实践了。 一,fso.GetFile 提取文件相应的 File 对象
1,getfile.asp
<% whichfile=Server.MapPath("cnbruce.txt") Set fso = CreateObject("Scripting.FileSystemObject") Set f1 = fso.CreateTextFile(whichfile,true) f1.Write ("This is a test.My Name is cnbruce.") f1.Close Set f2 = fso.GetFile(whichfile) s = "文件名称:" & f2.name & "<br>" s = s & "文件短路径名:" & f2.shortPath & "<br>" s = s & "文件物理地址:" & f2.Path & "<br>" s = s & "文件属性:" & f2.Attributes & "<br>" s = s & "文件大小: " & f2.size & "<br>" s = s & "文件类型: " & f2.type & "<br>" s = s & "文件创建时间: " & f2.DateCreated & "<br>" s = s & "最近访问时间: " & f2.DateLastAccessed & "<br>" s = s & "最近修改时间: " & f2.DateLastModified response.write(s) %>
其效果正如右键某文件,看到的具体属性信息。 其中Attributes返回的数值“32”表示:(Archive)上次备份后已更改的文件。可读写。
其它值附录如下:
Normal 0 普通文件。 没有设置任何属性。 ReadOnly 1 只读文件。 可读写。 Hidden 2 隐藏文件。 可读写。 System 4 系统文件。 可读写。 Directory 16 文件夹或目录。 只读。 Archive 32 上次备份后已更改的文件。 可读写。 Alias 1024 链接或快捷方式。 只读。 Compressed 2048 压缩文件。 只读。
二,file.move 作用将指定的文件或文件夹从某位置移动到另一位置。其实该方法仍然属于fso.GetFile后的一个应用。
2,movefile.asp
<% whichfile=Server.MapPath("cnbruce.txt") Set fso = CreateObject("Scripting.FileSystemObject") Set f1 = fso.CreateTextFile(whichfile,true) f1.Write ("This is a test.My Name is cnbruce.") f1.Close Set f2 = fso.GetFile(whichfile) f2.Move "C:\" %> <a href="C:\">查看下有没有</a>
简单的剪切粘贴的功能实现。
三,File.Copy 同样属于fso.GetFile后的一个应用。就只是单纯地拷贝文件到某位置。
3,copyfile.asp
<% whichfile=Server.MapPath("cnbruce.txt") Set fso = CreateObject("Scripting.FileSystemObject") Set f1 = fso.CreateTextFile(whichfile,true) f1.Write ("This is a test.My Name is cnbruce.") f1.Close Set f2 = fso.GetFile(whichfile) f2.Copy "D:\" %> <a href="D:\">查看下有没有</a>
和本ASP页面同在目录下的cnbruce.txt文件依然存在。
四,file.Delete 很显然,就是直接删除文件了。
4,delfile.asp
<% whichfile=Server.MapPath("cnbruce.txt") Set fso = CreateObject("Scripting.FileSystemObject") Set f1 = fso.CreateTextFile(whichfile,true) f1.Write ("This is a test.My Name is cnbruce.") f1.Close Set f2 = fso.GetFile(whichfile) f2.move "d:\" Set f3 = fso.GetFile("d:\cnbruce.txt") f3.delete %> <a href="d:\">查看下是没有该文件的</a>
当然FSO还没有结束,比如上传文件,ASP转HTML等都需要用到FSO。更精彩的依然是在后面。 |