VB Script:Add an entry in Hosts file


The hosts file is one of several system facilities to assist in addressing network nodes in a computer network.(As per the definition in Wikipedia!)
The hosts file contains lines of text consisting of an IP address in the first text field followed by one or more hostnames.

This file will be present in %SystemRoot%\system32\drivers\etc\

 

The below script is an example for adding an entry in to the hosts file.

This will append the existing hosts file. The same script can be used for updating the services file by changing the filename in the script.

No shy to say this is a copied script !!. I have modified some lines as per our requirement.

 

On Error Resume Next

Dim wshshell

Set wshshell = createobject("wscript.shell")

Line1="0.0.0.0  TEST"     ‘Enter the required entry here
Call AddValues(Line1)

Set wshShell = Nothing

 

Sub AddValues(Line)
    Dim varFile         ‘Variable to store the values of the file
    Dim varFileLines    ‘Variable to store the current line read from the file
    Dim WshShell        ‘Object pointing to the WScript
   
    ‘Initializing the WScript object
    Set WshShell = CreateObject("WScript.shell")
    ‘System root
    SysRoot = WshShell.ExpandEnvironmentStrings("%SystemRoot%")
    ‘File Path
    sFileName = SysRoot & "\system32\drivers\etc\Hosts"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(sFileName,1)

    Flag=0
   
    ‘looping through the hosts file
    Do Until objFile.AtEndOfStream
        ‘Reading a line from the hosts file
        varFileLines = objFile.ReadLine
        ‘Checking for the search string
        If Trim(varFileLines) <> Line Then
            ‘Building the string without the search string
            If Trim(varFile) = "" Then
                varFile = Trim(varFileLines)
            Else
                varFile = varFile & vbCrLf & Trim(varFileLines)
            End If
    Else
        Flag=1
        End If
    Loop
    ‘varFile = varFile & vbCrLf & Trim(varFileLines)
     ‘Closing the file object
        objFile.Close
    if Flag=0 then
        ‘varFile = varFile & vbCrLf & Trim(varFileLines) & vbCrLf & Line
        varFile = varFile & vbCrLf & Line
        ‘Creating the file with the search string
        Set objFile = objFSO.CreateTextFile(sFileName, True, False)
        ‘Writing the lines into the file
        objFile.WriteLine varFile
    ‘Closing the file object
    objFile.Close
    end if
    Set objFile = Nothing
End Sub

About these ads

0 Responses to “VB Script:Add an entry in Hosts file”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





Follow

Get every new post delivered to your Inbox.

Join 95 other followers

%d bloggers like this: