Hello,
I want to scan the hosts file for a specific entry and then report back to me if it is there. If it isn't i want it to be entered. The following script works. If the entry is not located it enters it. If the entry is located it reports back.
However when i add :
And a loop:
and an if statement :
The script now will find the first entry if it is there, if it isn't it will enter it. However it never finds the second pattern and always adds it even if it is there.
If i comment out all the code for the oRegEx then the bRegEx pattern works. It finds the entry or enters it if it cannot find it. So why does it not work with both in ?
I want to scan the hosts file for a specific entry and then report back to me if it is there. If it isn't i want it to be entered. The following script works. If the entry is not located it enters it. If the entry is located it reports back.
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\WINDOWS\system32\drivers\etc\hosts", ForReading)
Set objFileWrite = objFSO.OpenTextFile("C:\WINDOWS\system32\drivers\etc\hosts",ForAppending)
Set oRegEx = CreateObject("VBScript.RegExp")
oRegEx.Pattern = "^129\.111\.111\.20\tlinks\.link\.co\.uk"
oRegEx.Global = True
Const ForReading = 1
Const ForAppending = 8
Dim i
i=0
Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set aMatches = oRegEx.Execute(strSearchString)
If aMatches.Count > 0 Then
For Each strMatch in aMatches
Wscript.Echo strSearchString
i=2
Next
End IF
Loop
If i < 1 Then
objFileWrite.WriteLine("129.111.111.20 links.link.co.uk links")
WScript.Echo("Writing")
End If
However when i add :
Code:
Set bRegEx = CreateObject("VBScript.RegExp")
bRegEx.Pattern = "^129\.111\.111\.20\tblink\.blink\.co\.uk"
And a loop:
Code:
Dim a
a=0
Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set aMatches = bRegEx.Execute(strSearchString)
If aMatches.Count > 0 Then
For Each strMatch in aMatches
Wscript.Echo strSearchString
i=2
Next
End IF
Loop
and an if statement :
Code:
If i < 1 Then
objFileWrite.WriteLine("129.111.111.20 blink.blink.co.uk blinks")
WScript.Echo("Writing")
End If
The script now will find the first entry if it is there, if it isn't it will enter it. However it never finds the second pattern and always adds it even if it is there.
If i comment out all the code for the oRegEx then the bRegEx pattern works. It finds the entry or enters it if it cannot find it. So why does it not work with both in ?