?
Solved

Writing to the Hosts File

Posted on 2005-04-21
1
Medium Priority
?
141 Views
Last Modified: 2010-04-14
I need a MS DOS Batch file to write to the Hosts file. If there are existing entries in the Hosts file, how can I display these entries, and how can I append a new entry and how can I change an existing entry?
0
Comment
Question by:ben1211
1 Comment
 
LVL 97

Accepted Solution

by:
Lee W, MVP earned 1000 total points
ID: 13833352
REM Display Hosts File contents
If Exist %windir%\system32\drivers\etc\hosts type %windir%\system32\drivers\etc\hosts

REM Add a New Entry
Echo 123.45.6.78    newentry.domain.com >> %windir%\system32\drivers\etc\hosts

Rem Change an Entry - rename old hosts file to hosts.DateCodeTimeCode
for /f "tokens=1,2*" %%a in (%windir%\system32\drivers\etc\hosts) Do (
    If "%%a" == "123.45.6.79" (
        Echo 123.45.6.78 %%b %%c >> %windir%\system32\drivers\etc\hosts.new
    ) Else (
        Echo %%a %%b %%c >> %windir%\system32\drivers\etc\hosts.new
    )
)
If Exist %windir%\system32\drivers\etc\hosts ren %windir%\system32\drivers\etc\hosts %windir%\system32\drivers\etc\hosts.%date:~-4%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2    %%time:~6,2%
If Exist %windir%\system32\drivers\etc\hosts.new Ren %windir%\system32\drivers\etc\hosts.new %windir%\system32\drivers\etc\hosts
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Exchange administrators are always vigilant about Exchange crashes and disasters that are possible any time. It is quite essential to identify the symptoms of a possible Exchange issue and be prepared with a proper recovery plan. There are multiple…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question