ASP中FSO的功能2

下面来创建搜索页面。假设已经建立了一个HTML表单,用户在其中输入一个搜索字符串。

Dim objFolder
Dim strSearchText
Dim objFSO

strSearchText = Request.Form(“SearchText”) < -- The search string
' create the FSO and Folder objects
Set fso = Server.createObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("/"))

Search objFolder

   上面的代码简单地初始化变量,Search函数执行搜索功能,描述如下:

Function Search(objFolder)

Dim objSubFolder

'loop through every file in the current
folder

For Each objFile in objFolder.Files

Set objTextStream = objFSO.OpenTextFile(objFile.Path,1) < -- For Reading

'read the file's contents into a
variable

strFileContents = objTextStream.ReadAll

'if the search string is in the file, then
write a link

' to the file

If InStr(1, strFileContents, strSearchText, 1) then

Response.Write "< A HREF=""/" & objFile.Name & _

""">” & objFile.Name & “< /A>< BR>”

bolFileFound = True

End If

objTextStream.Close

Next

‘Here’s the recursion part – for each

‘ subfolder in this directory, run the Search function again

For Each objSubFolder in objFolder.SubFolders

Search objSubFolder

Next

End Function

   为了能打开文件,FSO需要实际的文件路径,而不是web路径。比如,是c:inetpubwwwroot empindex.html, 而不是
www.enfused.com/temp/index.html 或者 /temp/index.html。 为了将后者转换为前者,使用Server.MapPath
(“filename”), filename表示web路径名。

   上面的代码将在你指定的初始目录下的文件夹的每一个子目录中执行,在这里,初始目录是指web根目录“/”。然后
就简单地打开目录下的每一个文件,看看其中是否包含指定的字符串,如果找到字符串就显示那个文件的链接。

   注意,随着文件和子目录数量的增加,搜索花费的时间也将增加。如果需要繁重的搜索工作,建议你采取其他的方
法,比如微软公司的索引服务器Index Server。

   到此,你对FSO可能已经有了很好的体会。让我们再深入研究一步,来解决更复杂的难题。

   首先,你可能希望对文件改名。为了跟踪所有的文档,你将要重新命名它们以便唯一,这样就可以被系统容易地区
别。很不幸,FSO不允许简单的文件改名操作,所以我们不得不修改一下。

< %
' create the fso object
set fso = Server.createobject("Scripting.FileSystemObject")
path = "c: emp est.txt"
strDate = Replace(Date(), "/", "")
strDir = "c:inetpubwwwrootarticles" & strDate
strNewFileName = Hour(Now) & "_" & Minute(Now) & "_" &
second(Now) & ".html"

' open the old file
set file = fso.opentextfile(path, 1) < -- For reading
strText = file.readall
set file = nothing

' check for and/or create folder
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

' create and write new file
set file = fso.createtextfile(f.path & "" & strNewFileName)
file.write(strText)
set f = nothing
file.close
set file = nothing

' delete the old file
fso.deleteFile(path & "" & rst("FileName") & i)
' clean up
set fso = nothing
%>

   FSO能力的不足在这里却成了优势,我们可以一次执行2步。首先,打开文件并读入文件的内容。假设这里要创建一个
唯一的文件夹和一个唯一的文件来存储文章。然而,因为文件夹的路径每天都将改变,所以必须首先检查是否文件夹已经
存在,如果不存在,就创建它。这在if not fso.folderexists代码段完成。然后,取得那个路径,创建一个新的文件。新
文件建立完成后,删除掉旧文件,这通过fso.deleteFile来完成。

   这2步就是:对文件改名,然后移动到一个更合适的目录下。注意,在这里还可以对文件进行更多地操作,比如在写
入新文件前进行一下内容的编辑。

   FSO确实存在一些弱点 - 比如,它很难处理二进制文件,这包括Word文档、许多图形格式的文件和其他一些文件。然而你仍然可以用其他的方式操作这些文件 - 移动它们、删除它们,等等。你不能做的就是对它们进行打开或者写操作。
   另外一个限制是对于文件长度的问题。当立刻读写一些内容时,所有的信息都存储在内存中 - 内容越多,消耗的内存就越大。这将使每个工作都变得慢起来。所以,如果需要操作非常大的文件,或者大量的小文件时,考虑将文件分割成小块,并且经常的清除内存。将应用程序融入进COM对象组件,也能大大地提高程序的速度。

   同样,你也不能使用FSO来管理权限以及文件、文件夹的属性,执行安全加密的一个很好方法就是将前面提到的留言簿文件设置为只读,在需要时再设置为可写,然后再修改回来。这个方法经常在CGI和Perl使用,但是很不幸,还没有令人满意的方法来用FSO实现。

   还能用FSO做什么?

   在FSO中还有许多很棒的功能,但许多人没有意识到。这些功能常常是在你感到做某些事情很难后才发现的,这时你经常要感叹到:要是我早知道这个方法就好了!

   下面列举一下这些不常用但是却非常酷的功能:

   很少被了解的FSO功能
GetSpecialFolder Method 返回特定的Windows文件夹的路径: Windows安装目录;Windows系统目录;Windows临时目录 FSO.GetSpecialFolder([0, 1, or 2])
GetTempName Method 返回一个随机产生的文件或者目录名字,用于需要存储临时数据时
GetAbsolutePathName Method 返回文件夹的绝对路径(类似于Server.MapPath)。
比如,FSO.GetAbsolutePathName(“region”) 将返回类似于下面的结果:”c:mydocsmyfolder egion”
GetExtensionName Method 返回路径中最后部分的扩展名
(比如:FSO.GetExtensionName(“c:docs est.txt”) 将返回txt)
GetBaseName and GetParentFolder Methods 返回路径中最后部分的父文件夹
(比如:FSO.GetParentFolder (“c:docsmydocs”) 将返回’docs’)
Drives Property 返回所有本地可用驱动器的集合,用于建立资源浏览器样的用户接口。

   使用上面的功能时,最好建立好出错处理的代码。因为如果需要的参数不存在,将会产生麻烦的信息。

   总结

   如我们所见,FSO非常有用,这里介绍的仅仅是冰山一角。你可以使用FSO建立功能强大的应用程序,简单地完成许多任务

随机日志

发表评论

0 评论.

Leave a Reply



[ Ctrl + Enter ]

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word