如何在 vbscript 中的文件中间写入文本?

How to write text in the middle of a file in vbscript?

我想知道如何在 vbscript 中的文件中间写入文本。 文本文件有两行,一行是输出的名称,第二行是值。以“;”分隔的输出 例如 : 在插入文本之前,文本文件包含 -

mem1;mem2;mem3;
0.15;15.5;12.3;

插入新文本后 -

mem1;mem2;mem3;mem4
0.15;15.5;12.3;13.2

感谢您的帮助!

P.S。 - 请注意,它应该是 txt 文件而不是 csv。

我有这个片段:

'Usage: 
'If ReplaceInFile(filename, search, replace, addToEnd) = 0 Then 
'   WScript.Echo "Succeeded"
'End If

Function ReplaceInFile(strFilename, strSearch, strReplace, addToEnd)
    Dim fso, objFile, oldContent, newContent

    'Does file exist?
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(strFilename) = False Then
       ReplaceInFile = 0
       Exit Function
    End If

    'Read file
    Set objFile = fso.OpenTextFile(strFilename, 1)
    oldContent = objFile.ReadAll

    'Write file
    newContent = replace(oldContent, strSearch, strReplace, 1, 1, 0)
    newContent = newContent & addToEnd 
    Set objFile = fso.OpenTextFile(strFilename, 2)
    objFile.Write newContent
    objFile.Close 
    ReplaceInFile = 0
End Function

所以你可以这样使用:

If ReplaceInFile("your file path", ";\r\n", "mem4", "13.2") = 0 Then 
   WScript.Echo "Succeeded"
End If

警告!! 这里假设最后一行没有末尾换行!