<%
' ====================================================
' 文本文件操作函数库
' 功能:支持覆盖写入和追加内容
' ====================================================
' 函数:覆盖写入文件
' 参数:filePath - 文件路径(物理路径)
' content - 要写入的内容
' 返回:True表示成功,False表示失败
Function WriteToFile(filePath, content)
On Error Resume Next
Dim fso, file
Set fso = Server.CreateObject("Scripting.FileSystemObject")
' 创建文件(如果不存在则创建,存在则覆盖)
Set file = fso.CreateTextFile(filePath, True) ' True表示覆盖
file.Write content
file.Close
Set file = Nothing
Set fso = Nothing
If Err.Number = 0 Then
WriteToFile = True
Else
WriteToFile = False
Response.Write "错误:" & Err.Description
End If
End Function
' 函数:追加内容到文件
' 参数:filePath - 文件路径(物理路径)
' content - 要追加的内容
' 返回:True表示成功,False表示失败
Function AppendToFile(filePath, content)
On Error Resume Next
Dim fso, file
Set fso = Server.CreateObject("Scripting.FileSystemObject")
' 打开文件进行追加(第二个参数8表示追加模式)
Set file = fso.OpenTextFile(filePath, 8, True) ' 8=追加, True=文件不存在时创建
file.Write content
file.Close
Set file = Nothing
Set fso = Nothing
If Err.Number = 0 Then
AppendToFile = True
Else
AppendToFile = False
Response.Write "错误:" & Err.Description
End If
End Function
' 函数:读取文件内容
' 参数:filePath - 文件路径
' 返回:文件内容字符串
Function ReadFromFile(filePath)
On Error Resume Next
Dim fso, file, content
Set fso = Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(filePath) Then
Set file = fso.OpenTextFile(filePath, 1) ' 1=只读模式
content = file.ReadAll
file.Close
ReadFromFile = content
Else
ReadFromFile = "文件不存在"
End If
Set file = Nothing
Set fso = Nothing
End Function
' 函数:获取服务器物理路径
' 参数:relativePath - 相对路径
' 返回:物理路径
Function GetPhysicalPath(relativePath)
GetPhysicalPath = Server.MapPath(relativePath)
End Function
%>