使用asp完成txt文件转换操作。

来源:百度知道 编辑:UC知道 时间:2024/06/23 20:04:18
服务器上有一个txt文本文件,内容为:
123
455
123
等,固定格式。

想要把这个txt文件转换成为:
1,2,3
4,5,5
1,2,3
另存为txt文件保存在服务器上

下面是我为你写的函数ConvertFile(srcFileName,destFileName),srcFileName参数表示源文件相对路径,destFileName表示目的文件相对路径。如果转换成功,则返回值为0。

<%
Function ConvertFile(srcFileName,destFileName)
Dim fso,srcFileContent,ForReading, ForWriting
ForReading = 1
ForWriting = 2
srcFileName = Server.MapPath(srcFileName)
destFileName = Server.MapPath(destFileName)
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(srcFileName) Then
Set srcFile = fso.OpenTextFile(srcFileName, ForReading)
Set destFile = fso.CreateTextFile(destFileName, True)

While Not srcFile.AtEndOfStream
destFile.WriteLine(AddComma(srcFile.ReadLine))
Wend
Set destFile = nothing
Set srcFile = Nothing
ConvertFile = 0
Else
ConvertFile = 1 '返回值为1表示源文件不存在
End If
Set fso = Nothing
End Function

Function AddComma(srcLine)
Dim ResLine
srcLine=Trim(srcLi