title: Method of replacing a FULL LINE with text in a file?
body: <p>I'm looking for a way to replace a FULL LINE OF TEXT in a text file using VBScript. I've managed to get my code to find the line I want and replace some text in it, but as I said, I want the full line to be replaced.</p>
  
  <pre><code>'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 &lt; n Then
          before = objFile.ReadLine
          Call oldText
      ElseIf i = n Then
          current = objFile.ReadLine
          Call currentText
      ElseIf i &gt; 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"
  </code></pre>
  
  <p>For testing purposes I've commented out the deletion of the temp file so I can check and see if the changes have actually been made.</p>
  
  <p>Another method I chose was, if a certain file was specified, to copy a source file with pre-made changes into the same destination of the target file (overwriting it, just like the temp file would). The code for this is as follows:</p>
  
  <pre><code>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
  </code></pre>