ASP中FSO的神奇功能 - 用FSO进行内容管理
浏览:50日期:2022-12-11
平 到此,你对FSO可能已经有了很好的体会。让我们再深入研究一步,来解决更复杂的难题。 首先,你可能希望对文件改名。为了跟踪所有的文档,你将要重新命名它们以便唯一,这样就可以被系统容易地区别。很不幸,FSO不允许简单的文件改名操作,所以我们不得不修改一下。< %' create the fso objectset fso = Server.Createobject("Scripting.FileSystemObject")path = "c: emp est.txt"strDate = Replace(Date(), "/", "")strDir = "c:inetpubwwwrootarticles" & strDatestrNewFileName = Hour(Now) & "_" & Minute(Now) & "_" &second(Now) & ".html"' open the old fileset file = fso.opentextfile(path, 1);;< -- For readingstrText = file.readallset file = nothing' check for and/or create folderif not fso.folderexists(Server.MapPath(strDir)) thenset f = fso.CreateFolder(Server.MapPath(strDir))elseset f = fso.GetFolder(Server.MapPath(strDir))end if' create and write new fileset file = fso.Createtextfile(f.path & "" & strNewFileName)file.write(strText)set f = nothingfile.closeset file = nothing' delete the old filefso.DeleteFile(path & "" & rst("FileName") & i)' clean upset fso = nothing%> FSO能力的不足在这里却成了优势,我们可以一次执行2步。首先,打开文件并读入文件的内容。假设这里要创建一个唯一的文件夹和一个唯一的文件来存储文章。然而,因为文件夹的路径每天都将改变,所以必须首先检查是否文件夹已经存在,如果不存在,就创建它。这在if not fso.folderexists代码段完成。然后,取得那个路径,创建一个新的文件。新文件建立完成后,删除掉旧文件,这通过fso.DeleteFile来完成。 这2步就是:对文件改名,然后移动到一个更合适的目录下。注意,在这里还可以对文件进行更多地操作,比如在写入新文件前进行一下内容的编辑。
相关文章:

