Question : How to create Batch File to Add Entires to Host File?

How could a create a simple batch file that will add entries to my Host File in XP or Vista, so I can block a site like www.abc.com.

Answer : How to create Batch File to Add Entires to Host File?

Here is one a little closer to your stated objective.

Paste the script below into a text file with a .vbs extension.  Customize the desired new entries as indicated below line 10.  Running the script will check to see if the entries already exist in the HOSTS file.  If not it will add them.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
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
Open in New Window Select All
Random Solutions  
 
programming4us programming4us