FileSystemObject 对象用于访问服务器上的文件系统。
FileSystem和TextStream对象可用于建立对文件系统的访问,并提供顺序访问文件的机制。FileSystem没有属性,只有两个方法,第一个方法是CreateTextFile方法,可以在宿主机上创建新的文本文件,并返回TextStream对象以提供对新创建文件的访问机制。第二个是OpenTextFile方法,用于打开文本文件供顺序访问并返回一个TextStream对象.
= = = = = = = = = = = 分 割 线 = = = = = = = = = = == = =
CreateTextFile 方法
定义和用法
CreateTextFile 方法可在当前文件夹中创建新的文本文件,并返回可用于读或写文件的 TextStream 对象。
语法:
FileSystemObject.CreateTextFile(filename[,overwrite[,unicode]]) FolderObject.CreateTextFile(filename[,overwrite[,unicode]])
参数 | 描述 |
---|---|
filename | 必需的。需创建文件的名称。 |
overwrite | 可选的。指示能否覆盖已有文件的布尔值。True 指示可覆盖文件,False 指示不能覆盖文件。默认是 True 。 |
unicode | 可选的。指示文件是作为 Unicode 还是 ASCII 文件来创建的布尔值。True 指示文件作为 Unicode 文件创建,而 False 指示文件被作为 ASCII 文件创建。默认是 False。 |
针对 FileSystemObject 对象的实例
<%
dim fs,tfile
set fs = Server.CreateObject( " Scripting.FileSystemObject " )
set tfile = fs.CreateTextFile( " c:\somefile.txt " )
tfile.WriteLine( " Hello World! " )
tfile.close
set tfile = nothing
set fs = nothing
%>
dim fs,tfile
set fs = Server.CreateObject( " Scripting.FileSystemObject " )
set tfile = fs.CreateTextFile( " c:\somefile.txt " )
tfile.WriteLine( " Hello World! " )
tfile.close
set tfile = nothing
set fs = nothing
%>
= = = = = = = = = = = 分 割 线 = = = = = = = = = = == = =
OpenTextFile 方法
OpenTextFile 方法打开指定的文件,并返回可用来访问此文件的 TextStream 对象
语法:
FileSystemObject.OpenTextFile(fname,mode,create,format)
参数 | 描述 |
---|---|
fname | 必需的。要打开的文件的名称。 |
mode | 可选的。如何打开文件。
|
create | 可选的。设置如果文件名不存在,是否创建新文件。True 指示可创建新文件,而 False 指示新文件不会被创建。False 是默认的。 |
format | 可选的。文件的格式。
|
实例
<%
dim fs,f
set fs = Server.CreateObject( " Scripting.FileSystemObject " )
set f = fs.OpenTextFile(Server.MapPath( " testread.txt " ), 8 , true )
f.WriteLine( " This text will be added to the end of file " )
f.Close
set f = Nothing
set fs = Nothing
%>
dim fs,f
set fs = Server.CreateObject( " Scripting.FileSystemObject " )
set f = fs.OpenTextFile(Server.MapPath( " testread.txt " ), 8 , true )
f.WriteLine( " This text will be added to the end of file " )
f.Close
set f = Nothing
set fs = Nothing
%>
= = = = = = = = = = = 分 割 线 = = = = = = = = = = == = =
访问统计
ManageLogStat.asp
<table class="tableBorder" width="55%" border="0" align="center" cellpadding="5" cellspacing="1">
<form action="DelcontentStat.asp" method="post">
<tr>
<th width="50%">访问统计</th>
<th>作弊参数</th>
</tr>
<tr>
<td class="forumRow" align="center">
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("../count/count.txt"), 1)
Stat = f.ReadAll
Response.Write(Stat)
f.Close
Set f=Nothing
Set fs=Nothing
%>
</td>
<td align="center" class="forumRow">
<input type="text" name="Stat" maxlength="20" value="<%=Stat%>" />
</td>
</tr>
<tr>
<td class="forumRow">
</td>
<td class="forumRow">
<input type="submit" value="修改" />
</td>
</tr>
</form>
</table>
DelContentStat.asp
<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.CreateTextFile(Server.MapPath("../count/count.txt"),true)
Stat = request("Stat")
f.write(Stat)
f.close
set f=nothing
set fs=nothing
response.redirect request.servervariables("http_referer")
%>