Writing to the Hosts File

Posted on 2005-04-21
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?
Question by:ben1211
    1 Comment
    LVL 95

    Accepted Solution

    REM Display Hosts File contents
    If Exist %windir%\system32\drivers\etc\hosts type %windir%\system32\drivers\etc\hosts

    REM Add a New Entry
    Echo >> %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" == "" (
            Echo %%b %%c >> %windir%\system32\drivers\etc\
        ) Else (
            Echo %%a %%b %%c >> %windir%\system32\drivers\etc\
    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\ Ren %windir%\system32\drivers\etc\ %windir%\system32\drivers\etc\hosts

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    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…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now