computing
  • 1

Batch To Edit/Delete An IP From Hosts File.

  • 1

Hi —

I’m in need of some assistance in creating a batch file that will edit/delete an IP out of the Host file.
—————–
I just started learning on how to create batch files so try and give a small explanation on your help please…
Here’s what I have so far.

@echo off

echo 1 is for Denver

echo 2 is for Los Angeles

set input=
set /p input=enter your option –

if %input%==1 goto 1
if %input%==2 goto 2

:1
echo 192.168.X.XX test.test.com>>c:\Windows\System32\drivers\etc\hosts
echo Host file has been set to point to Denver.
pause

:2
echo Host file has been set to point to Los Angeles.
pause
—————–

As you can see…
-I have it set to where prompt asks you where you want your host file pointing.
-When the user selects 1, it updates the Hosts file with the IP address wanted.

Now, I want option 2 to remove that IP address, that way it reverts the Hosts file back to normal.

Any help is appreciated.

Share

1 Answer

  1. @echo off & setlocal enabledelayedexpansion
    set hosts=%windir%\system32\drivers\etc\hosts
    set entry=192.168.X.XX test.test.com
    echo.
    findstr /i “test.test.com” %hosts%>nul
    if %errorlevel% == 0 (
    echo Host entry for denver exists.. Checking active status
    findstr /i “test.test.com” %hosts% | findstr /i “#” >nul
    echo.
    if !errorlevel! == 0 echo Entry is inactive
    if !errorlevel! == 1 echo Entry is active.
    )
    echo.
    echo 1 is for Denver
    echo 2 is for Los Angeles

    set /p input= Enter your choice :

    if %input%==1 (
    findstr /i /v “test.test.com” %hosts% >%temp%\hostback.txt
    type %temp%\hostback.txt >%hosts%
    echo 192.168.X.XX test.test.com >>%hosts%

    echo Host file is pointing to denvor now
    )

    if %input%==2 (
    echo you choosed LA, disabling the value.
    findstr /i /v “test.test.com” %hosts% >%temp%\hostback.txt

    type %temp%\hostback.txt >%hosts%
    echo #192.168.X.XX test.test.com >>%hosts%
    echo done!!
    Echo.. checking and confirming..
    findstr /i “test.test.com” %hosts% | findstr /i “#” >nul
    echo.
    if %errorlevel% == 0 echo Host file disabled successfully
    )

    Subhash Chandra.

    • 0