< % Set fso = Server.CreateObject("Scripting.FileSystemObject") % > |
FSO方法 | |
CopyFile | 拷贝一个或者多个文件到新路径 |
CreateTextFile | 创建文件并且返回一个TextStream对象 |
DeleteFile | 删除一个文件 |
OpenTextFile | 打开文件并且返回TextStream对象,以便读取或者追加 |
< html> < body> < form action="formhandler.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> |
< % ' Get form info strName = Request.Form("username") strHomePage = Request.Form("homepage") strEmail = Request.Form("Email") ' create the fso object Set fso = Server.CreateObject("Scripting.FileSystemObject") |
path = "c: emp est.txt" ForReading = 1, ForWriting = 2, ForAppending = 3 ' open the file set file = fso.opentextfile(path, ForAppending, TRUE) ' write the info to the file file.write(strName) & vbcrlf file.write(strHomePage) & vbcrlf file.write(strEmail) & vbcrlf ' close and clean up file.close set file = nothing set fso = nothing |
回想一下,OpenTextFile方法返回一个TextStream对象,它是FSO模型中的另外一个对象。TextStream对象揭示了操作文件内容的方法,比如写、读一行、跳过一行。VB常量vbcrlf产生一个换行符。
在OpentextFile的命令参数中定义了TRUE,这就告诉了系统,如果文件不存在,就创建它。如果文件不存在,并且没有定义TRUE参数,就会出错。
现在转到目录c: emp,打开test.txt,你可以看到如下的信息:
User's name User's home page User's email |
< % ' create the fso object set fso = Server.Createobject("Scripting.FileSystemObject") path = "c: emp est.txt" ' open the file set file = fso.opentextfile(path, 1) < -- For reading |
do until file.AtEndOfStream Response.write("Name: " & file.ReadLine & " ") Response.write("Home Page: " & file.ReadLine & " ") Response.write("Email: " & file.ReadLine & "< p>") loop ' close and clean up file.close set file = nothing set fso = nothing %> |
Server object error 'ASP 0177 : 800a003e' |
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 |
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 %> |
新闻热点
疑难解答