Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
On Error Resume Next
Set objEntries = CreateObject("Scripting.Dictionary")
objEntries.CompareMode = VBTextCompare
'Add new entries here
objEntries.Add "www.abc.com", "127.0.0.1"
objEntries.Add "www.def.com", "127.0.0.1"
Set objShell = CreateObject("WScript.Shell")
strSystemRoot = objShell.ExpandEnvironmentStrings("%systemroot%")
strHostsFile = strSystemRoot & "\system32\drivers\etc\hosts"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strHostsFile, ForReading, False, TriStateUseDefault)
strText = objTextFile.ReadAll
arrText = Split(strText, vbCrLf)
objTextFile.Close
strWhiteSpace = " " & vbTab & vbCrLf & vbCr & vbLf
For Each strEntry in objEntries.Keys
blnFound = False
For Each strLine in arrText
intPos = InStr(1, strLine, strEntry, vbTextCompare)
If intPos > 0 And Left(strLine, 1) <> "#" Then
If InStr(strWhiteSpace, Mid(strLine, intPos - 1, 1)) Then
If intPos + Len(strEntry) = Len(strLine) + 1 Or InStr(strWhiteSpace, Mid(strLine, intPos + 1, 1)) > 0 Then
blnFound = True
End If
End If
End If
Next
If Not blnFound Then
strText = strText & vbCrLf & objEntries.Item(strEntry) & vbTab & strEntry
End If
Next
Set objTextFile = objFSO.OpenTextFile(strHostsFile, ForWriting)
objTextFile.Write strText
objTextFile.Close
|