用文件中的文本替换FULL LINE的方法?

时间:2016-01-07 11:18:56

标签: replace vbscript

我正在寻找一种使用VBScript替换文本文件中的FULL LINE OF TEXT的方法。我设法让我的代码找到我想要的行并替换其中的一些文本,但正如我所说,我希望更换整行。

'Finds and Replaces some text in a file using line numbers
Const ForReading = 1
Const ForWriting = 2

strFileName = WScript.Arguments(0)
n = WScript.Arguments(1)
strNewText = WScript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")

objFSO.CreateTextFile "temp"

Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

For i = 1 to 37
    If i < n Then
        before = objFile.ReadLine
        Call oldText
    ElseIf i = n Then
        current = objFile.ReadLine
        Call currentText
    ElseIf i > n Then
        after = objFile.ReadLine
        Call newText
    End If
Next

Sub oldText
    Set objTemp = objFSO.OpenTextFile("temp", 8) 'Open Text file for appending
    objTemp.WriteLine before
    objTemp.Close
End Sub

Sub currentText

    ' ########################################################################
    ' This is where my problem is - I don't know how to replace the whole line
    ' with my strNewText variable. As seen below - I'm not that familiar with
    ' VBScript, so I done a little tinkering:
    ' ########################################################################

    'change = Replace(current,  , strNewText) 'won't change anything
    'change = Replace(current, "" , strNewText) 'won't change anything
    'change = Replace(current, strNewText) 'won't change anything either
    'change = Replace(current.all, strNewText) 'nope
    'change = Replace(current, current, strNewText) 'no-dice=veryTrue

    'This works but only for where "block" appears in the line. I haz full
    'line plz?
    change = Replace(current, "block", strNewText)

    Set objTemp = objFSO.OpenTextFile("temp", 8)
    objTemp.WriteLine change
    objTemp.Close
End Sub

Sub newText
    Set objTemp = objFSO.OpenTextFile("temp", 8)
    objTemp.WriteLine after
    objTemp.Close
End Sub

objFSO.CopyFile "temp", strFileName, True

'objFSO.DeleteFile "temp"

出于测试目的,我已经删除了临时文件,因此我可以检查并确定是否实际进行了更改。

我选择的另一种方法是,如果指定了某个文件,则将具有预先更改的源文件复制到目标文件的同一目标(覆盖它,就像临时文件一样)。代码如下:

strFileName = WScript.Arguments(0)
n = CLng(WScript.Arguments(1))
strNewText = WScript.Arguments(2) 'This is also used as the folder name when changing eula. Just need to rename variable to something more readable.
tempFileName = "temp"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTemp = objFSO.OpenTextFile(tempFileName, 2, True)
Set objFile = objFSO.OpenTextFile(strFileName)

If strFileName = "eula.txt" Then
    objFSO.CopyFile "sourc file path\eula.txt", "C:root folder\"+strNewText+"\eula.txt", True 'Paths are typed in actual code.
Else
    Do Until objFile.AtEndOfStream
        If objFile.Line = n Then
            objTemp.WriteLine strNewText
        Else
            objTemp.WriteLine objFile.ReadLine
        End If
    Loop

    objFile.Close
    objTemp.Close

    objFSO.CopyFile tempFileName, strFileName, True
End If

1 个答案:

答案 0 :(得分:2)

您无需更换任何东西。只需将strNewText而不是current写入输出文件即可。如果您需要依赖于输入文件中的行是否包含特定(子)字符串,您可以使用If条件:

If InStr(current, "block") > 0 Then
  objTemp.WriteLine strNewText
Else
  objTemp.WriteLine current
End If

我建议的其他一些事情:

  • 打开和关闭文件很昂贵。不要一遍又一遍地这样做。在循环之前打开输出文件一次,并在写完所有内容后关闭它。
  • 使用Do Until循环处理输入文件,这样您就不需要知道确切的行数。
  • 如果您需要检查特定的行号,可以使用文件对象的Line属性,该属性具有接下来要读取的行号,而不是自己保留索引。

修改后的代码:

strFileName = WScript.Arguments(0)
n = CLng(WScript.Arguments(1))
strNewText = WScript.Arguments(2)
tempFileName = "temp"

Set objFSO = CreateObject("Scripting.FileSystemObject")

'create temp file and open for writing
Set objTemp = objFSO.OpenTextFile(tempFileName, 2, True)
Set objFile = objFSO.OpenTextFile(strFileName)

Do Until objFile.AtEndOfStream
    If objFile.Line = n Then
        current = objFile.ReadLine
        If InStr(current, "block") > 0 Then
            objTemp.WriteLine strNewText
        Else
            objTemp.WriteLine current
        End If
    Else
        objTemp.WriteLine objFile.ReadLine
    End If
Loop

objFile.Close
objTemp.Close

objFSO.CopyFile tempFileName, strFileName, True