< % 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 %> |
新聞熱點
疑難解答