Best way to fix a Windows Host file with combined entries?

Posted on 2004-11-10
Last Modified: 2010-05-18
One of the guys had created a Bat file that was to append a new IP and Name (which was in a file called "new_ip.txt" to the end of the Hosts file on either 95 or W2k/XP with the following script...
echo off
if exist %windir%\hosts.sam goto win95
if exist %windir%\system32\drivers\etc\hosts goto win2kxp

copy %windir%\hosts.sam+c:\new_ip.txt %windir%\hosts.sam
goto exit

copy %windir%\system32\drivers\etc\hosts+c:\new_ip.txt %windir%\system32\drivers\etc\hosts
goto exit

REM Please close this window
This apparently worked when he tested it before deployment; but now I'm getting calls about people not being able to get to servers and have diagnosed the problem back to their Host files and the fact that his script works for all unedited Host files that had a carriage return at the end by default, but those that had been edited and saved with new entries, did not.  Thus it caused the two strings to be joined together on one line, so...
"IP1.IP1.IP1.IP1      Name1" in an edited host file became
"IP1.IP1.IP1.IP1      Name1IP2.IP2.IP2.IP2            Name2"
and now I'm left to try and clean this up.

My question would be, how could this script be changed or a new one developed to 1) go back and fix this error and 2) make sure any new ones get done properly?

IP2 and Name 2 are known values, but IP1 and Name1 could be anything and would need to be left with the new entry beneath it.  Thanks.
Question by:AZEMBB
    LVL 6

    Expert Comment

    Why not use a dns server  ?

    Author Comment

    DNS would have been nice, but the desktops are spread out worldwide on various networks and domains which are not able to be administered.
    LVL 5

    Accepted Solution


    Try this link, it deals with exactly what you want.  It has a download with pre scripted files in, and instructions on how to configure.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Many of us in IT utilize a combination of roaming profiles and folder redirection to ensure user information carries over from one workstation to another; in my environment, it was to enable virtualization without needing a separate desktop for each…
    Trying to figure out group policy inheritance and which settings apply where can be a chore.  Here's a very simple summary I've written which might help.  Keep in mind, this is just a high-level conceptual overview where I try to avoid getting bogge…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    746 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

    14 Experts available now in Live!

    Get 1:1 Help Now