ASP 中可利用 FSO 对文本文件进行操作,QuickFSO 封装了常用 FSO 的常用功能,旨在提供便捷的文本文件操作。本版本为 v0.1,仅包含两个函数:ReadFile、WriteFile。
<%
'ITPOW www.itpow.com
'QuickFSO v0.1 MIT
'对 FSO 常用功能进行封装。
'读取文件的全部内容。
'若文件不存在,则产生异常。
'示例:Response.Write(ReadFile(Server.MapPath("/foo.txt")))
function ReadFile(filePath)
ReadFile = ""
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
'第二个参数 1 表示只读打开;第三个参数表示目标文件不存在时是否创建
set f = fso.OpenTextFile(filePath, 1, false)
ReadFile = f.ReadAll()
f.Close()
set f = nothing
set fso = nothing
end function
'向文件写入内容。
'若文件不存在,则自动创建。
'示例:call WriteFile(Server.MapPath("/foo.txt"), "123"&vbcrlf&"456")
function WriteFile(filePath, content)
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
'第二个参数 2 表示重写,如果是 8 表示追加;第三个参数表示目标文件不存在时是否创建
set f = fso.OpenTextFile(filePath, 2, true)
f.Write(content)
f.Close()
set f = nothing
set fso = nothing
end function
%>