夜火最近学校里讲了ASP的FSO组件,没听太懂。。。上课打瞌睡了~,以前自己也看过一些关于ASP的书,所以就算打瞌睡最后的作业也顺利完成交上去了。学校教的进度实在是不敢恭维,还是自己多找点关于FSO的东西来充实下,顺便当作笔记,以备后查
FSO是FileSystemObject 或 Scripting.FileSystemObject 的缩写,为 IIS 内置组件,用于操作磁盘、文件夹或文本文件。FSO 的对象、方法和属性非常的多,这里用示例的方式列出常用的,注意:《VBScript 语言参考》或《JScript 语言参考》中的:《FileSystemObject 用户指南》和《Scripting 运行时库参考》便是微软给出的 FileSystemObject 完整参考。
FSO 不能操作二进制文件,要操作二进制文件,使用:ADODB.Stream。
创建文件
1dim fso, f2set fso = server.CreateObject("Scripting.FileSystemObject")3set f = fso.CreateTextFile("C: est.txt", true) '第二个参数表示目标文件存在时是否覆盖4f.Write("写入内容")5f.WriteLine("写入内容并换行")6f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)7f.Close()8set f = nothing9set fso = nothing
打开并读文件
1dim fso, f2set fso = server.CreateObject("Scripting.FileSystemObject")3set f = fso.OpenTextFile("C: est.txt", 1, false) '第二个参数 1 表示只读打开,第三个参数表示目标文件不存在时是否创建4f.Skip(3) '将当前位置向后移三个字符5f.SkipLine() '将当前位置移动到下一行的第一个字符,注意:无参数6response.Write f.Read(3) '从当前位置向后读取三个字符,并将当前位置向后移三个字符7response.Write f.ReadLine() '从当前位置向后读取直到遇到换行符(不读取换行符),并将当前位置移动到下一行的第一个字符,注意:无参数8response.Write f.ReadAll() '从当前位置向后读取,直到文件结束,并将当前位置移动到文件的最后9if f.atEndOfLine then10 response.Write("一行的结尾!")11end if12if f.atEndOfStream then13 response.Write("文件的结尾!")14end if15f.Close()21 collapsed lines
16set f = nothing17set fso = nothing18
19**打开并写文件**dim fso, f20set fso = server.CreateObject("Scripting.FileSystemObject")21set f = fso.OpenTextFile("C: est.txt", 2, false) '第二个参数 2 表示重写,如果是 8 表示追加22f.Write("写入内容")23f.WriteLine("写入内容并换行")24f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)25f.Close()26set f = nothing27set fso = nothing28
29**判断文件是否存在**dim fso30set fso = server.CreateObject("Scripting.FileSystemObject")31if fso.FileExists("C: est.txt") then32 response.Write("目标文件存在")33else34 response.Write("目标文件不存在")35end if36set fso = nothing
移动文件
1dim fso2set fso = server.CreateObject("Scripting.FileSystemObject")3call fso.MoveFile("C: est.txt", "D: est111.txt") '两个参数的文件名部分可以不同4set fso = nothing
复制文件
1dim fso2set fso = server.CreateObject("Scripting.FileSystemObject")3call fso.CopyFile("C: est.txt", "D: est111.txt") '两个参数的文件名部分可以不同4set fso = nothing
删除文件
1dim fso2set fso = server.CreateObject("Scripting.FileSystemObject")3fso.DeleteFile("C: est.txt")4set fso = nothing
创建文件夹
1dim fso2set fso = server.CreateObject("Scripting.FileSystemObject")3fso.CreateFolder("C: est") '目标文件夹的父文件夹必须存在4set fso = nothing
判断文件夹是否存在
1dim fso2set fso = server.CreateObject("Scripting.FileSystemObject")3if fso.FolderExists("C:Windows") then4 response.Write("目标文件夹存在")5else6 response.Write("目标文件夹不存在")7end if8set fso = nothing
删除文件夹
1dim fso2set fso = server.CreateObject("Scripting.FileSystemObject")3fso.DeleteFolder("C: est") '文件夹不必为空4set fso = nothing