/build/static/layout/Breadcrumb_cap_w.png

Insert text in SERVICES file with validation

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. 
Thanks


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
Else  
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

objTextFile.Close
objTextFile2.Close

Set objTextFile = objFSO.OpenTextFile("C:\Windows\system32\drivers\etc\Services", ForWriting)
objTextFile.WriteLine strNewFile
objTextFile.Close

0 Comments   [ + ] Show comments

Answers (1)

Posted by: dedenker 10 years ago
3rd Degree Black Belt
0
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.


Comments:
  • Use a Dictionary instead. They're much faster than arrays and the syntax is a little more friendly, too. - anonymous_9363 10 years ago
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ