Jump to content
Sign in to follow this  

HOSTS File Line Insert

Recommended Posts


I've been beating my head trying to resolve a problem and I'm hoping someone may have an answer.

I need to insert 1 or 2 lines (most likely 1 line) into the HOSTS file without overwriting any existing entries that may already be in the HOSTS file and I need the script to be able to do this no matter which version of Windows (XP, Vista or 7) the end user has on their PC.

Note: The IP(s) and hostname(s) I am trying to insert are known values.

I seen several threads about working with the HOSTS file but none of the ones I've read are what I am looking for.

Any suggestions would be greatly appreciated.

Share this post

Link to post
Share on other sites

And where ist your problem?

1. Getting the path

2. Reading the file (maybe obsolete if you just add as last line)

3. adding your stuff

4. done

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post

Link to post
Share on other sites

What about this?

$strHost = @SystemDir & "\drivers\etc\hosts"
$tcpIP = @IPAddress1
$sWhat = "localhost"

$hFile = FileOpen($strHost, 1) ; The 1 parameter just appends to the end of the file
FileWrite($hFile, $tcpIP & " " & $sWhat)

Thanks everyone for such quick responses..


I'll give your script a try but it looks exactly like what I need.

Share this post

Link to post
Share on other sites

You may need to change this line:

FileWrite($hFile, $tcpIP & " " & $sWhat)
FileWrite($hFile, @LF & $tcpIP & " " & $sWhat)
if the line doesn't get written to the right place.

Thanks again James,

I noticed that the first time I tested the script that it appened to the end of the existing line.

It's working great, I just need to get it working for Windows 7 but that should just be it's UAC blocking the changes.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.