Edit host file by Batch File

Update Host file with bath script

Today I got a mail from one of my senior is there a way by which we can add multiple address in host file with out consuming lot of time and my simple reply was we need to use batch file for that. They said ok do it but actually I have never done it before so I had no other option than Google.


I searched for Google but every time I got a lengthy result and comment on those results were not positive it worked for some people and for some people it doesn't. I was wondering If I can have a simplest way for this as my previous backup script.


So I decided if I can print few lines in host file and it worked.Now no need of writing a long batch script for editing host file.


Here is the command


echo 192.168.1.10 test.test.com>>c:\Windows\System32\drivers\etc\hosts


You can add as many line as you want to add and save the file as hostentry.bat

Let me explaint you meaning of each word here


Echo it will print the words written after it

192.168.1.10 Its the ip of the server

Test.test.com Its the dns name of the server

>> it save the printed data in given file

c:\Windows\System32\drivers\etc\hosts Host file location for printg the data


Needless to say that IP and domain name would get change as per your server detail

if your oprating system is not in c driver please chagne the drive latter as per needed.



Just run the batch file and you are done for updating your host entry.



Hope it was usefull for you.



Please let me know if you face any problem in it.

23 comments:

Anonymous said...

Thank you.

Just what I was looking for.

Theo

Anonymous said...

thanks a TON....

Anonymous said...

how to change the hosts file to do this:

redirect

www.yahoo.com to my.yahoo.com

its a problem as the ip from www.yahoo.com is equal to my.yahoo.com

anyone a solution???

Adil said...

Thanx Buddy
It's really works

adil said...

hey can u tell how to edit in group policy via batch file.
my id is adil@adilfahim.com
really it's urgent.

Anonymous said...

gr8 work

Anonymous said...

thank's bro ....

worldofrugs said...

Almost exactly what I'm looking for...
However, How can you check first if it already exists? And if not, write the line, if it does, quit.
Example:

if 192.168.1.10 test.test.com in c:\Windows\System32\drivers\etc\hosts Exitst

then do nothing
else

echo 192.168.1.10 test.test.com>>c:\Windows\System32\drivers\etc\hosts

Any suggestions?

Andrés said...

worldofrugs i think you can do that using c++
reading / writing on the host file.

my Q? here is, is there a way to ERASE a line?

Anonymous said...

if the batch runs multiple times it keeps adding in the same lines. is there a way for it to not run again after it runs once?

Unknown said...

We were also looking for an easy command-line way to edit the hosts file, not only adding entries but also deleting them.

Since echo will never give you that, I wrote a small command line tool that does exactly that: add and remove entries from the hosts file. It also does not allow duplicates.

you can find it on github: teamaton toolbelt,
just click on the download button and choose the HostsEditor.exe.

You can also fiddle with the source code, it's all accessible.

Have fun,
Oliver

Anonymous said...

Amazing just what I have been looking for just make sure you run as Administrator

Oliver said...

Hi all,

we also needed to edit the hosts file programmatically and adding lines using echo was just not enough.

After reading several comments where people ask how to avoid duplicates and erase lines, I wrote a small tool that does all of that: the teamaton HostsEditor.
You can read more details on our dev blog: a simple hosts file editor.
It is an .exe that you can call from the command line and you can verify the source code on GitHub.

I hope someone out there will find this useful.

Oliver said...

Just one more thing: for some it might not be obvious but on our GitHub toolbelt page you need to click on the Download button or use the following link to directly download the HostsEditor.exe.

Kiran Nayak said...

I want to replace old yahoo messengers host server by current host server.
(I dont want to upgrade my messenger)

If anybody knows how to do it, then please contact me at s_kiran_nayak@yahoo.co.uk

Anonymous said...

you ar good man !!!

Anonymous said...

Thanks, just what I needed.

ysh_fdez said...

Very useful!

echo. >>c:\Windows\System32\drivers\etc\hosts
echo 127.0.0.1 demo>>c:\Windows\System32\drivers\etc\hosts

With "echo." we put a line break and avoid having two hosts on the same line (invalidating the first).

Anonymous said...

THANK YOU!!!

Infork22 said...

its not working :( please anyone help me i need it urgently.
I just tested what its given. I copied your command, pasted it on notepad and saved it as a hostentry.bat file on my desktop and then double click on that..right?
Its nothing updated in host file.
I am on Windows XP.
Please help me how to do it. It will be great if anyone mail the procedure to me on my email id or post it here i will read it.

infork22@gmail.com

Yan said...

Thanks for this. Though there were a lot others which offers more functionality, it is more complicated. I stick to this one since it successfully do it's job.

NaJeEb303 said...

it works thanxx,, but can you provide a single batch script that wil delete specific lines from the hosts file .. thanxx

Anonymous said...

Grewt post! We are linking to this great post on our
website. Keep up the greazt writing.

my webpage ... just click the following post

Clicky Web Analytics