title: Append text to text file if it already exists
body: <p>I have a script which is working, to replace some characters in a fixed width file (starting from row 2 onward). </p>
  
  <p>What want to avoid overwriting the target file if it already exists. Instead, if it exists, to append the rows (from row 2 onwards of the source file) to the end of the target file. I am struggling to find a thread with a proper suggestion. This is the current code:</p>
  
  <pre><code>Dim objFSO
  dim objFile
  dim thisLine
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  
  
  If (objFSO.FileExists("C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat")) Then
    Set objFile = objFSO.GetFile("C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat")
  Else
    WScript.Quit()
  End If
  
  If objFile.Size &gt; 0 Then 'make sure the input file is not empty
      Set inputFile = objFSO.OpenTextFile("C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat", 1)  'Replace the filename here
      set outputFile = objFSO.CreateTextFile("C:\Users\Dimitar\Desktop\BPSDRC\PAYIMP.dat", TRUE) 'replace it with output filename
  
      ' first line - leave it as it is
      thisLine = inputFile.ReadLine  
      newLine = thisLine
      outputFile.WriteLine newLine
  
      'all remaining lines - read them and replace the middle part with 18 zeroes 
      do while not inputFile.AtEndOfStream  
              thisLine = inputFile.ReadLine  ' Read an entire line into a string.
              'the zeroes are to fix issue N1 (payment in other amt)
              'the CDF are to fix issue N2 (payment in local amt)
              newLine = mid(thisLine,1,47) &amp; "000000000000000000" &amp; mid(thisLine,66,121) &amp; "CDF" &amp; mid(thisLine,190)
              outputFile.WriteLine newLine
      loop
      inputFile.Close
      outputFile.Close
      objFSO.DeleteFile "C:\Users\Dimitar\Desktop\BPSDRC\PAYBOTH.dat"
  end if
  </code></pre>