Batch to edit/delete an IP from Hosts file.

February 15, 2011 at 17:53:39
Specs: Windows XP
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.


See More: Batch to edit/delete an IP from Hosts file.

Report •


#1
February 15, 2011 at 18:02:55
It might be a little hard to edit a file without seeing what it looks like...

Can I put a question here?


Report •

#2
February 15, 2011 at 18:28:13
Not sure I understand...

I mean option 1 actually does work, it writes to the Hosts file with the information provided. But I'm having trouble reverting it back...


Report •

#3
February 15, 2011 at 22:36:53
Ok i tested it. you have a problem with your code.
after label 1 at the end of the label code i.e
: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
goto :eof

otherwise it still completes the form. so after you have chosen option 1 it. you can press a key to continue, and then when you press enter it tells you that the host has been set to Los Angeles.

and then with your original Q.

what i would have done, i would put it in the code to make a backup of the file, executing the commands and then if option 2 is chosen to copy the backup, without any changes

D_Pikster


Report •

Related Solutions

#4
February 16, 2011 at 11:36:10
Thanks for the reply. I took your advise and made a directory "backup" and copy the 'hosts' file into it.

So now Option 2 looks like this:
-----
:2
copy C:\WINDOWS\system32\drivers\etc\Backup\hosts "C:\WINDOWS\system32\drivers\etc"
echo Host file has been set to point to Los Angeles.
pause
-----

Question though how can i make the bat create the directory and copy the hosts file ONCE? Like for example If add this line to the top of my .bat it will keep copying the hosts file to the backup folder every time i run it.


@echo off

mkdir C:\WINDOWS\system32\drivers\etc\Backup
copy C:\WINDOWS\system32\drivers\etc\hosts "C:\WINDOWS\system32\drivers\etc\Backup"

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.1.10 test.test.com>>c:\Windows\System32\drivers\etc\hosts
echo Host file has been set to point to Denver.
pause
goto:eof


:2
copy C:\WINDOWS\system32\drivers\etc\Backup\hosts "C:\WINDOWS\system32\drivers\etc"
echo Host file has been set to point to Los Angeles.
pause


---------------

There a way to make it check if that file already exists, don't replace?


Report •

#5
February 16, 2011 at 11:55:26
Just change the upper portion of your code to include:


If exist C:\WINDOWS\system32\drivers\etc\Backup GOTO SKIPBKUP
mkdir C:\WINDOWS\system32\drivers\etc\Backup
copy C:\WINDOWS\system32\drivers\etc\hosts "C:\WINDOWS\system32\drivers\etc\Backup"
:SKIPBKUP


Report •

#6
February 16, 2011 at 12:02:21
Perfect. Thank you.


Thanks everyone that helped. It is much appreciated.


Report •

#7
February 16, 2011 at 13:07:22
@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.


Report •

#8
February 16, 2011 at 19:00:39
Holy smokes. Very nice. Way beyond what I know (haha), but works perfect. Exactly what I needed. Thank you. I will continue learning now that I've found this place. :)

Report •

#9
February 16, 2011 at 19:25:35
well, happy to help..thaks for posting though ..overall we learn from you guys :)

Subhash Chandra.


Report •

#10
February 16, 2011 at 19:35:58
@D_Pikster ,
Sir, just so you know hosts file in windows acts like a local DNS server. Wonder everytime you click on a weblink on the internet or open a website the browser has to make a DNS query (well,DNS is Beyond our topic) and before it calls the DNS it reads the hosts file. If it finds an entry it use that otherwise a long process begins to find the correct IP.

the # means comments in this file, in your reply instead of creating backup if you just have deleted that file that will work as well..most often nowawadys this file is not used. Hosts are going to be history like WINS soon

Subhash Chandra.


Report •

Ask Question