📄 fso.txt
字号:
文件系统对象组件
FSO模块来自微软提供的脚本运行库scrrun.dll中。
FSO对象包括
驱动器对象(Drive Object)用来存取本地盘或网络盘,
文件系统对象(FielSystemObject 简称FSO)是用来存取文件系统
文件夹对象(Folder Object)用于存取文件夹的各种属性
文本流对象(TextStream Object 简称TS)存取文件内容(Write,ReadLine,SkipLine)
FSO要使用物理绝对路径,而不是Web 的相对路径。
例如,要打开c:\inetpub\wwwroot\index.html,而不能用www.127.0.0.1/index.html 。要将后一种路径转换为物理绝对路径,使用Server.MapPath("index.html")。
如何使用FSO?
1. 建立文件对象
变量fso为文件对象名
<%
Set fso = Server.CreateObject("scripting.FileSystemObject")
%>
2. 使用
通过“object.方法”来使用它
方法:
CopyFile 拷贝一个或多个文件
CreateTextFile 创建一个文件并返回一个TS对象
DeleteFile 删除一个文件
OpenTextFile 打开一个文件并返回一个可用于读和添加的TS对象。
例1 写文件
假设要从用户提交的form中收集信息
<html>
<body>
<form action="rec.asp" method="post">
<input type="text" size="10" name="username">
<input type="text" size="10" name="homepage">
<input type="text" size="10" name="Email">
</form>
</body>
</html>
rec.asp
<%
' 获取 form信息
strName = Request.Form("username")
strHomePage = Request.Form("homepage")
strEmail = Request.Form("Email")
' 建立fso对象
Set fso = Server.CreateObject("scripting.FileSystemObject")
path = Server.MapPath("test.txt") 设置文件路径
' 打开文件
set file = fso.opentextfile(path, ForAppending, TRUE)
' 操作参数
ForReading = 1, ForWriting = 2, ForAppending = 3
' 参数TRUE表示当文件不存在时生成一个新文件,如果不用TRUE,当文件不存在时就会返回一个错误
' 把信息写入由对象变量file表示的文件
file.write(strName) & vbcrlf vbcrlf是换行符
file.write(strHomePage) & vbcrlf
file.write(strEmail) & vbcrlf
' c:\temp\test.txt加入3行数据,内容为用户的输入(本例每3行为一个用户的信息)
' 关闭并清除对象
file.close
set file = nothing
set fso = nothing
例2 读文件,输出信息
首先要知道数据结构,假定test.txt文件每3行为一个用户信息
AtEndOfStream是TS对象的一个属性,让你知道是否到达文件结尾。
<%
' 建立fso对象
set fso = Server.Createobject("scripting.FileSystemObject")
path = Server.MapPath("test.txt") 设置文件路径
'打开文件
set file = fso.opentextfile(path, 1)
'操作参数 ForReading = 1, ForWriting = 2, ForAppending = 3
' 使用循环 file.ReadLine读出文件中的一行数据
' AtEndOfStream是属性,判定文件指针是否结束
do until file.AtEndOfStream
Response.write("Name: " & file.ReadLine & " ")
Response.write("Home Page: " & file.ReadLine & " ")
Response.write("Email: " & file.ReadLine & "<p>")
loop
' 关闭并清除
file.close
set file = nothing
set fso = nothing
%>
3. FSO的权限问题
FSO的读写权限就是用户账号所拥有的权限。例如用administrator本机登录并访问页面,那么建立的FSO就拥有administrator的权限。
Internet账号(IUSER_机器名)通常只有读权限,无写权限。
由于在Internet上访问的用户都是以anonymous登录,无写权限。为此可建立一个允许账号为 IUSER_机器名 的用户读写的目录。但这样可能会带来安全漏洞。最好将这个目录建立在Web路径之外。
4. 用FSO实现查找
用FSO在Web站点上实现查找功能。建立搜索引擎的关键是递归。
首先来建立搜索页面。假设已经给用户提供了一个输入搜索字符串的页面。
Dim objFolder
Dim strSearchText
Dim objFSO
strSearchText = Request.Form("SearchText") <-- 用户输入的关键字
' 建立 FSO 和文件夹对象
Set fso = Server.CreateObject("scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("/"))
'定义函数Search
Function Search(objFolder)
Dim objsubFolder
For Each objFile in objFolder.Files '循环搜寻当前文件夹中的每个文件
Set objTextStream = objFSO.OpenTextFile(objFile.Path,1) <-- 1 Reading
'把文件内容读入一个变量
strFileContents = objTextStream.ReadAll
'如果在文件中找到该字符串,写个指到该文件的链接
If InStr(1, strFileContents, strSearchText, 1) then
Response.Write "<A HREF=""/" & objFile.Name &""">" & objFile.Name & "</A><BR>"
bolFileFound = True
End If
objTextStream.Close
Next
'以下是递归部分 如果有子目录,再调用Search函数
For Each objsubFolder in objFolder.SubFolders
Search(objsubFolder)
Next
End Function
该程序会自动搜索Web root目录以下的所有子目录.
5. FSO其他功能
GetSpecialFolder 方法
返回一个特殊的Windows文件目录:Windows安装文件目录,系统文件目录,临时文件目录。使用方法分别是:FSO.GetSpecialFolder([0, 1, or 2>)
GetTempName 方法
返回一个随机生成的临时文件或文件夹。
GetAbsolutePathName 方法
返回一个文件夹的绝对路径。例如,FSO.GetAbsolutePathName("region")会返回象"c:\mydocs\myfolder\region" 这样的路径。
GetExtensionName 方法
返回文件的扩展名。例如 FSO.GetExtensionName("c:\docs\test.txt") 返回 "txt"。
GetBaseName 和 GetParentFolder 方法
分别返回根目录名和父目录名。例如 FSO.GetParentFolder ("c:\docs\mydocs") 返回 "docs"。
Drives 属性
返回本机上所有驱动器的集合。如果你要建立一个explorer风格的界面,这个功能再有用不过。
6. FSO功能上的弱点
在处理二进制文件的
内存占用
不能改变文件和文件夹的属性。
FSO不允许直接对文件改名。可先新建一个文件,用fso.DeleteFile 将旧文件删除。
<%
' 建立fso对象
set fso = Server.Createobject("scripting.FileSystemObject")
path = "c:\temp\test.txt"
strDate = Replace(Date(), "/", "")
strDir = "c:\inetpub\wwwroot\articles\" & strDate
strNewFileName = Hour(Now) & "_" & Minute(Now) & "_" &second(Now) & ".html"
' 打开旧文件
set file = fso.opentextfile(path, 1)
strText = file.readall
set file = nothing
' 检查是否要建立文件夹,由于用户可能将文件改名到另一个目录下,所以必须判断新目录是否存在
if not fso.folderexists(Server.MapPath(strDir)) then
set f = fso.CreateFolder(Server.MapPath(strDir))
else
set f = fso.GetFolder(Server.MapPath(strDir))
end if
' 创建并写入新文件
set file = fso.Createtextfile(f.path & "\" & strNewFileName)
file.write(strText)
set f = nothing
file.close
set file = nothing
' 删除旧文件
fso.DeleteFile(path & "\" & rst("FileName") & i)
' 清除
set fso = nothing
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -