下面是一个利用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对象的属性和方法!