Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1273
  • Last Modified:

Deleting and Adding an Entry from the hosts file.

I need a batch file that will delete an entry from the hosts file. The hosts file that I have looks like this:

________________________________________________________________________
# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost

#128.8.2.35      b035
#128.8.2.250     klse-b  server
10.1.12.135      c135
10.1.12.98       klse-c server
10.1.12.89        pamserver1
#10.1.7.35        pamserver2
__________________________________________________________________________________________

1. How can I delete an entry from the hosts file? Say I want to delete the entire entry for 10.1.12.89. So how do I delete the entire line of entry?

2. Once I have deleted this line of entry, how do I add an additional line of entry say 10.1.7.23   pamserver.pam.com into the hosts file?

Thank You
0
ben1211
Asked:
ben1211
  • 4
  • 4
1 Solution
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
REM The following will find all lines in the file hosts NOT containing "10.1.12.89"
REM This is done through the use of the /v switch.  The /i switch ignores CaSe.
REM The end takes the found lines and directs them to the new file, hosts.new
find /v /i "10.1.12.89" %windir%\system32\drivers\etc\hosts > hosts.new
REM Then we delete the existing hosts file
del hosts
REM Then we rename the new file to be THE hosts file.
ren hosts.new hosts
REM To add an entry, simply do this:
Echo 10.1.7.23  pamserver.pam.com >> %windir%\system32\drivers\etc\hosts
REM the above line will append a line to the hosts file containing the information
REM between the word Echo and the >> characters.
0
 
LongbowCommented:
hosts file have normally the read only attribute.
I will first create a backup in case of...

copy %windir%\system32\drivers\etc\hosts %windir%\system32\drivers\etc\hosts.bak
attrib -R %windir%\system32\drivers\etc\hosts
Type %windir%\system32\drivers\etc\hosts.bak | find /V "The.IP.You.Want2Delete" > %windir%\system32\drivers\etc\hosts
attrib +R %windir%\system32\drivers\etc\hosts
pause
0
 
Leon FesterCommented:
Why don't you just create the files that you need with the updated hosts information and then overwrite the existing file?

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Longbow -

Backup is always a good idea.  But hosts is NOT usually read only - unless you "protect" it with something (perhaps Spybot).  It is NTFS security wise Read Only, in which case Attrib won't work - nor will my script unless the user is an admin.
0
 
LongbowCommented:
You are correct for Win XP, perhaps for Windows 2000 too.
Not for NT4 and maybe Windows 98.
This is not an XP or 2000 thread and Ben1211 don't talk us about wich OS he uses.
Then independently of his OS i gave a solution wich works on all Windows platforms.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
How can your script work for all OSs when the attrib command would do nothing on XP and 2000?  (I'd have to check NT4) and the path to the hosts file is different in 98.
0
 
LongbowCommented:
What's your aim Leew ?
I am not asking help at all on this.
Try my .bat
It works an all NT Systems.
And even your hosts file is read-only or not, it does nothing but no errors are displayed.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
My AIM is to help the user and not mislead him.

You said:
> Then independently of his OS i gave a solution wich works on all Windows platforms.
This is blatently not true.  Your solution will NOT work on Win9x or Me.  It can't because th host file will not be there.  Mine won't work for these systems either, but I'm not saying it will.

> attrib -R %windir%\system32\drivers\etc\hosts
Again, the Attribute is NOT set and the file permission IS set. Explain how this can change NTFS permissions?  In my experience and understanding, it simply cannot.  Thus, for normal user accounts and at least in 2000 and XP, this is pointless and accomplishes nothing.


You also said:
> I am not asking help at all on this.
I'm trying to help the requestor.  If you are absolutely certain, then TEACH ME SOMETHING.  I'm open to learn, but I require intelligent debate and evidence before I will take the word of someone who I know little if anything about (you have nothing in your profile so I don't know your possible skill level).

> Try my .bat
> It works an all NT Systems.
> And even your hosts file is read-only or not, it does nothing but no errors are displayed.

It may work on all NT *4* systems (I have my doubts but since I don't have an NT 4 system handy, I'm not arguing that).  BUT, it's a fairly safe bet he is not using NT4.  Aside from the fact it is 9 years old, the copy of a hosts file he posted has a copyright date of 1999, and, excluding his apparent additions, the text he posted is the same as included with Windows 2000 and XP.
0
 
LongbowCommented:
Hi Leew,
I posted a solution wich "works an all NT Systems."
Please forget the "all Windows platforms."
Most of the time i do not answer questions about Win9x.
I do not have the time.
I have 15 years of experience. I began in February 1990.
I am not at the right place to dabate.
Regards.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now