2016-01-07 27 views
0

我正在尋找一種方法來使用VBScript替換文本文件中的完整文本行。我已經設法讓我的代碼找到我想要的行並替換它中的一些文本,但正如我所說的,我希望完整行被替換。使用文件中的文本替換FULL LINE的方法?

'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 

回答

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 
+0

優秀點佈局很好。 – Lankymart

+0

感謝Asgar的幫助!我已經接受了你的建議,並試圖在不復制的情況下對其進行編碼(所以我可以更容易地學習它),但是我開始遇到問題並最終將你提供的內容完全複製到單獨的.vbs文件中無濟於事。從命令行運行時,該腳本將運行並完成。在命令窗口中它看起來很好。但是,當我檢查文件 - 沒有更改。我也檢查了臨時文件,並且在那裏也沒有做出更改。我對代碼做了一些修改來測試它,並收集問題所在。續...所有的 – whiteside0013

+0

首先,我想在'添加參數 '對讀' 設置OBJFILE = objFSO.OpenTextFile(strFileName,真)'所以就成了'設置OBJFILE = objFSO.OpenTextFile(strFileName,1,真)' 。我也嘗試將If語句中的條件修改爲If InStr(current,「block」)= 0 Then',基本上繞過了條件(我認爲)。但是,這仍然不起作用。 – whiteside0013