Announcement

Collapse
No announcement yet.

Batch File Help Needed (Host file)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch File Help Needed (Host file)

    To all experts out there, i need some help regarding a Batch file command. And i am not very sure whether even is there such a command out there.

    I am try to write batch file (Below) to add a sentence into the host file automatically when the user double click the host.bat.

    echo. >> "%systemroot%\system32\drivers\etc\hosts"
    echo 127.0.0.1 localhost >> "%systemroot%\system32\drivers\etc\hosts"


    But the question is, is there a command like when the users double click on the host.bat the next day, the host.bat will check for the existence of this "127.0.0.1 localhost" in the host file. If this "127.0.0.1 localhost" is existing, it will just end without adding another sentence in the host.

    If don't exist it will add.

    All the experts out there, please help me.

  • #2
    Re: Batch File Help Needed (Host file)

    Code:
    cls
    type "%systemroot%\system32\drivers\etc\hosts" | find "127.0.0.1 localhost" > nul
       if not errorlevel = 1 goto found
    
    echo. >> "%systemroot%\system32\drivers\etc\hosts"
    echo 127.0.0.1 localhost >> "%systemroot%\system32\drivers\etc\hosts"
    
    :done
    :found
    exit
    \Rem

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Batch File Help Needed (Host file)

      Cool Thanksyou yor help man... really appreciate your expertise on this matter.

      Thanks

      Comment


      • #4
        Re: Batch File Help Needed (Host file)

        I believe you need to use "if not errorlevel 1 goto ..." (no "=")
        Cheers,

        Rick

        ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

        2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

        Comment


        • #5
          Re: Batch File Help Needed (Host file)

          Rick you are right, the = shouldn't be there.
          But is will be not of a problem if it is there, because Windows wil simply ignore this "single =" .

          To vampiel_6
          Windows xp has even a better command for FIND that is called FINDSTR.
          In your case you should better use FINDSTR /B
          http://www.robvanderwoude.com/index.html
          Then you know for sure the line: 127.0.0.1 localhost
          is there and that line is not set as REMark in any ways.


          \Rem

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment

          Working...
          X