I need to insert information in the file C:\Windows\system32\drivers\etc\Services but checking first if the data is not already there. My script is reading the information from another file, verify if exists, but is just inserting the first row, even though is checking the whole document. Any help will be appreciated. 

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Windows\system32\drivers\etc\Services", ForReading) 'services files
Set objTextFile2 = objFSO.OpenTextFile("C:\temp\Insert.txt", ForReading) 'file with the strings to insert
Dim value

Do Until objTextFile2.AtEndOfStream
strNextLine2 = objTextFile2.Readline

Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline

intLineFinder = InStr(strNextLine, strNextLine2)
If intLineFinder <> 0 Then
value = 1
strNextLine = strNextLine2
strNextmp = strNextLine2
strNewFile = strNewFile & strNextLine & vbCrLf
value = 0
strNextLine = strNextLine
End If

strNewFile = strNewFile & strNextmp & vbCrLf ' 
Loop ' Second Do

If value = 1 Then 
strNewFile = strNewFile & vbCrLf & strNextLine2 
value = 0
End If
Loop ' First Do


Set objTextFile = objFSO.OpenTextFile("C:\Windows\system32\drivers\etc\Services", ForWriting)
objTextFile.WriteLine strNewFile
0 Comments   [ - ] Hide Comments


Please log in to comment

Answer this question or Comment on this question for clarity


You have to create an array for all the line that need to be added, see MS technet.
So add the lines if missing to the array.
Then add all the lines from array to file.

Answered 07/09/2014 by: dedenker
Third Degree Black Belt

  • Use a Dictionary instead. They're much faster than arrays and the syntax is a little more friendly, too.
Please log in to comment