下面是一個利用FSO創建文件,和向裏面寫入數據的實例教程!
<% Dim fs, serverPath, filePath, txts Set fs = Server.CreateObject("Scripting.FileSystemObject") ForAppending = 8 '打開用於讀寫 serverPath = Server.MapPath(".") '映射系統物理路徑,Server.MapPath(".")表示當前目錄 filePath = serverPath & "/GAMVANfile/" '獲取物理路徑在是新建文件存在當前目錄下GAMVANfile文件夾裏面! if not fs.FolderExists(filePath) then fs.CreateFolder(filePath) end if '檢測文件夾是否存在,沒有則自動創建! txts = "<hr height='1'>春眠不覺曉,<br>處處蚊子咬!"&_ "<br><br><a href=''>www.GAMVAN.com</a>"&_ "<br><br>更新時間:" & now() & "<hr>" Dim f, fName fName = filePath&"GAMVAN.htm" if fs.FileExists(fName) then Set f = fs.OpenTextFile(fName, ForAppending, True) else Set f = fs.CreateTextFile(fName, ForWriting, false) end if '上面七行代碼是判斷fName文件是否存在如果存在就打開它用於讀寫,否則就自動創建 f.Write(txts) f.Close Set f = Nothing Response.Write fName & " 創建成功!" %> |
其實上面這個代碼就是靜態網站發佈系統的最原始的原理,那些可以生成HTML的新聞發佈系統都是依循這個原理設計的……
關於TextSteam其他屬性方法就是大同小異,依此例類推即可!
以上源代碼在 WIN XP + IIS5.0 測試通過!
接下來的文章我們將介紹File、Folder、Driver對象的屬性和方法!