给你一个应用FileSystemObject的例子,
关于FileSystemObject在ASP中的用法
---- 在ASP编程过程中,我们有时需要对文件进行操作。用户可以使用 File Access 组件创建 FileSystemObject 对象,该对象提供用于访问文件系统的方法、属性和集合。通过文件操作,可以实现计数器,留言板等功能。以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读取或写入:
testfile=Server.Mappath("testfile.txt")
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(testfile, True)
a.WriteLine("这是一个测试。")'写一行
a.Close
---- 说明:a是返回的TextStream 对象,它有以下这些方法:
Close Read Readall ReadLine Skip SkipLine
Write WriteLine WriteBlankLines
---- 各方法的详细用法参见Microsoft Visual Basic Scripting Edition (VB 脚本语言参考)
---- 以上代码中,CreateObject 函数返回 FileSystemObject 对象 (fs)。CreateTextFile 方法创建一个文件作为 TextStream 对象 (a),然后 WriteLine 方法在此文件中写入一行文本。最后 Close 方法刷新缓冲区并关闭文件。
---- FileSystemObject 提供对计算机文件系统的访问。这个对象有以下方法:
BuildPath CopyFile CopyFolder CreateFolder
CreateTextFile DeleteFile DeleteFolder DriveExists
FileExists FolderExists GetAbsolutePathname GetBaseName
GetDrive GetDriveName GetExtensionName GetFile GetFileName
GetFolder GetParentFolderName GetSpecialFolder
GetTempName MoveFile MoveFolder OpenTextFile
---- 各方法的详细用法参见Microsoft Visual Basic Scripting Edition (VB 脚本语言参考)。从其提供的方法可以看到,FileSystemObject 对象文件功能是非常全面的,可以完成拷贝,删除,查找,列目录,察看文件等等功能。通过使用这些方法和属性,有人实现了类似于资源管理器ASP程序,从远程通过网络管理服务器上的文件。
---- 下面介绍一个用FileSystemObject实现的一个计数器程序,在介绍计数器之前,先介绍一下打开文件和建立文件的语法:
---- OpenTextFile 语法:
object.OpenTextFile(filename[, iomode[, create[, format]]])
---- filename必选,指定文件名;iomode可选,输入/输出模式, 是下列两个常数之一:ForReading=1或ForAppending=8;Create可选,允许创建新文件时为 True,否则为 False;format可选,指出以何种格式打开文件。三个值,-2以系统默认格式打开文件, -1以 Unicode 格式打开文件,0以 ASCII 格式打开文件。若忽略此参数,则文件以 ASCII 格式打开。
---- CreateTextFile语法:
object.CreateTextFile(filename[, overwrite[, unicode]])
---- filename必选,指定文件名;overwrite可选,true为覆盖现有文件方式,false为不覆盖, 对于 filename 已经存在的文件,如果 overwrite 参数为 False,或未提供此参数时,则会出现错误;unicode可选,true是以Unicode 文件格式创建文件,false是以ASCII 文件格式创建文件,默认false。
---- FileSystemObject的典型应用:计数器程序,可以产生像如下的计数器:
---- 准备:做好数字的图片0~9.jpg(0.jpg是数字0的图片,上面的00210是由asp程序用0.jpg、1.jpg与2.jpg组合而成的),假设计数文件名为count.txt,和counter.asp在一个目录下,counter.asp是计数器程序,test.asp引用此计数器,浏览器访问test.asp就可以刷新计数器。Count.txt的权限为"everyone更改"。
---- test.asp的内容如下:
< !--#INCLUDE File="counter.asp"-- >
< %'引用计数器% >
< html >< head >< /head >
< body >
< %counter(5)
'调用counter.asp中的counter过程,输出5位计数器
% >
< body >
< /html >
---- counter.asp的内容如下:
< SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER >
sub counter(n)
Countfile=Server.Mappath("count.txt")
'将计数文件映射为物理路径
Set fs = CreateObject("Scripting
.FileSystemObject")'产生文件访问对象
Set a = fs.OpenTextFile(Countfile,1,false,false)
'以只读,不覆盖,ASCII方式打开文件
visitors=a.ReadLine'读出一行访问记录
visitors=visitors+1 '访问记录加一
length=len(visitors)
t0=n-length
for t=1 to t0 '计数器高位输出0
response.write("
< img src=counter/img/p0.jpg >")
next
t=1
do while t< =length '输出计数值
strURL="< img src=counter/img
/p"+mid(visitors,t,1)+".jpg >"
response.write(strURL)
t=t+1
loop '生成图像 URL
a.close
set a=fs.createTextFile(Countfile,true)
a.writeline(visitors) '计数器加一后写入文件
a.close
end SUB
< /SCRIPT >
---- FileSystemObject 对象可以实现大多数与文件有关的功能,只要你多思考,多实践,灵活应用,会发现很多新的用法。
---------------------------------------------------------------
制作一个个人搜索引擎(源码)
<%
Response.Buffer=True
'
' OneFile Search Engine (ofSearch v1.0)
' Copyright ?000 Sixto Luis Santos
1<[email protected]>
2' All Rights Reserved
3'
4' Note:
5' This program is freeware. This program is NOT in the Public Domain.
6' You can freely use this program in your own site.
7'
8' You cannot re-distribute the code, by any means,
9' without the express written authorization by the author.
10'
11' Use this program at your own risk.
12'
13
14
15' Globals --------------------------------------
16' ----------------------------------------------
17
18Const ValidFiles = "htmltxt"
19Const RootFld = "./"
20
21Dim Matched
22Dim Regex
23Dim GetTitle
24Dim fs
25Dim rfLen
26dim RootFolder
27Dim DocCount
28Dim DocMatchCount
29Dim MatchedCount
30
31' ----------------------------------------------
32' Procedure: SearchFiles()
33' ----------------------------------------------
34Public Sub SearchFiles(FolderPath)
35Dim fsFolder
36Dim fsFolder2
37Dim fsFile
38Dim fsText
39Dim FileText
40Dim FileTitle
41Dim FileTitleMatch
42Dim MatchCount
43Dim OutputLine
44
45' Get the starting folder
46Set fsFolder = fs.GetFolder(FolderPath)
47' Iterate thru every file in the folder
48For Each fsFile In fsFolder.Files
49' Compare the current file extension with the list of valid target files
50If InStr(1, ValidFiles, Right(fsFile.Name, 3), vbTextCompare) > 0 Then
51DocCount = DocCount + 1
52' Open the file to read its content
53Set fsText = fsFile.OpenAsTextStream
54FileText = fsText.ReadAll
55' Apply the regex search and get the count of matches found
56MatchCount = Regex.Execute(FileText).Count
57MatchedCount = MatchedCount + MatchCount
58If MatchCount > 0 Then
59DocMatchCount = DocMatchCount + 1
60' Apply another regex to get the html document's title
61Set FileTitleMatch = GetTitle.Execute(FileText)
62If FileTitleMatch.Count > 0 Then
63' Strip the title tags
64FileTitle = Trim(replace(Mid(FileTitleMatch.Item(0),8),"","",1,1,1))
65' In case the title is empty
66If FileTitle = "" Then
67FileTitle = "No Title (" & fsFile.Name & ")"
68End If
69Else
70' Create an alternate entry name (if no title found)
71FileTitle = "No Title (" & fsFile.Name & ")"
72End If
73' Create the entry line with proper formatting
74' Add the entry number
75OutputLine = " <b>" & DocMatchCount & ".</b> "
76' Add the document name and link
77OutputLine = OutputLine & "<a "="" ")="" &="" \","="" chr(34)="" href=" & chr(34) & RootFld & replace(Mid(fsFile.Path,
78rfLen),"><b>"
79OutputLine = OutputLine & FileTitle & "</b></a>"
80' Add the document information
81OutputLine = OutputLine & "<font size="1"><br/> Criteria matched " & MatchCount
82& " times - Size: "
83OutputLine = OutputLine & FormatNumber(fsFile.Size / 1024,2 ,-1,0,-1) & "K bytes"
84OutputLine = OutputLine & " - Last Modified: " & formatdatetime
85(fsFile.DateLastModified,vbShortDate) & "</font><br/>"
86' Display entry
87Response.Write OutputLine
88Response.Flush
89End If
90fsText.Close
91End If
92Next
93
94' Iterate thru each subfolder and recursively call this procedure
95For Each fsFolder2 In fsFolder.SubFolders
96SearchFiles fsFolder2.Path
97Next
98
99Set FileTitleMatch = Nothing
100Set fsText = Nothing
101Set fsFile = Nothing
102Set fsFolder2 = Nothing
103Set fsFolder = Nothing
104End Sub
105
106' ----------------------------------------------
107' Procedure: Search()
108' ----------------------------------------------
109Sub Search(SearchString)
110Dim i
111Dim fKeys
112Dim fItems
113
114Set fs = CreateObject("Scripting.FileSystemObject")
115Set GetTitle = New RegExp
116Set Regex = New RegExp
117
118With Reg</[email protected]>