Board logo

标题: VBS--谁写个txt文件删除添加某行的调用啊 [打印本页]

作者: sexfio     时间: 2010-1-20 17:17    标题: VBS--谁写个txt文件删除添加某行的调用啊

比如c:\1.txt
a
b
c
我要删除第2行返回
a
c

或我要添加第2行,添加数据是*,返回
a
*
b
c

谁给写个简洁点的Function吧
作者: qinchun36     时间: 2010-1-21 10:53

DeleteLine "c:\1.txt", 2
InsertLine "c:\1.txt", 2, "*"

Sub DeleteLine(filespec, rownum)
        Dim fso, f, temp, i
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set f = fso.OpenTextFile(filespec, 1)
        temp = ""
        i = 0
        While Not f.AtEndOfStream
                i = i + 1
                If Not i = rownum Then
                        temp = temp & f.ReadLine & vbCrLf
                Else
                        f.SkipLine
                End If
        WEnd
        f.Close
        Set f = fso.OpenTextFile(filespec, 2)
        f.Write temp
        f.Close
End Sub


Sub InsertLine(filespec, rownum, oneline)
        Dim fso, f, temp, i
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set f = fso.OpenTextFile(filespec, 1)
        temp = ""
        i = 0
        While Not f.AtEndOfStream
                i = i + 1
                If i = rownum Then temp = temp & oneline & vbCrLf
                temp = temp & f.ReadLine & vbCrLf
        WEnd
        f.Close
        Set f = fso.OpenTextFile(filespec, 2)
        f.Write temp
        f.Close
End Sub

作者: HAT     时间: 2010-1-21 13:36
sed很好很简洁