Top Integrated Developer Environments (IDEs)

Top Integrated Developer Environments (IDEs): 51 Powerful Dev Environments for Streamlined Development

 

Advertisements

insert a line into a text file after a specific line

from

https://community.spiceworks.com/scripts/show/1709-add-a-line-of-text-into-a-file

briefly:

set objWS = CreateObject("Wscript.Shell")
Set fsob=CreateObject("Scripting.FileSystemObject")

strNewLine = "INSERT LINE TO ADD"
strFileName = "INSERT THE FILE PATH HERE" ' for example c:\textfile.txt

Const FOR_READING = 1
Const FOR_WRITING = 2
strCheckForString = UCase("NEW LINE WILL BE INSERTED AFTER THIS TEXT")
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(strFileName, FOR_READING)
strContents = objTS.ReadAll
objTS.Close
arrLines = Split(strContents, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)
For Each strLine In arrLines
  If (Left(UCase(LTrim(strLine)),Len(strCheckForString)) = strCheckForString) Then
    objTS.WriteLine strLine
    objTS.WriteLine strNewLine
  else
    objTS.WriteLine strLine
  End If
Next