append/change hosts file with a bat file

QuintusSmit
QuintusSmit used Ask the Experts™
on
Hi

I would like to make a .bat file that will change the value  in my hosts file "192.168.0.254   Mywhatever" to "193.168.0.1 Mywhatever" and back again when the batch file is run a second time.

Is this possible?

Thank you
 
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Brian PiercePhotographer
Awarded 2007
Top Expert 2008

Commented:
Most Valuable Expert 2011
Top Expert 2011

Commented:
In a batch file

For 1 address

@echo off
 copy /Y \\server\share\hosts.txt c:\windows\system32\drivers\etc\hosts
exit

Then

@echo off
 copy /Y \\server\share\hosts1.txt c:\windows\system32\drivers\etc\hosts
exit

Commented:
I would think the best bet would be something like :


if not exists c:\windows\system32\drivers\etc\hosts.bak GOTO toggleOn
GOTO toggleOff

:toggleon
copy c:\windows\system32\drivers\etc\hosts hosts.bak
echo 193.168.0.1 Mywhatever >> c:\windows\system32\drivers\etc\hosts
goto EOF

:toggleOff
del c:\windows\system32\drivers\etc\hosts
copy c:\windows\system32\drivers\etc\hosts.bak hosts
del c:\windows\system32\drivers\etc\hosts.bak

If the hosts.bak file doesn't exist, it copies the host file and calls it hosts.bak, then edits the original hosts file. If hosts.bak exists, it deletes hosts, and copies hosts.bak to hosts, then removes the hosts.bak.
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Commented:
I haven't tested this but it should get you started.

Priz
xcopy %SystemRoot%\system32\drivers\etc\hosts %StstemRoot%\system32\drivers\etc\hosts.bak /Y
findstr /v <ipdress|hostname> %SystemRoot%\system32\drivers\etc\hosts > %SystemRoot%\system32\drivers\etc\hosts.tmp
echo <newIP>[TAB]<newhost> >> %SystemRoot%\system32\drivers\etc\hosts.tmp
xcopy %SystemRoot%\system32\drivers\etc\hosts.tmp %StstemRoot%\system32\drivers\etc\hosts /Y

<Do your other stuff here>

xcopy %SystemRoot%\system32\drivers\etc\hosts.bak %StstemRoot%\system32\drivers\etc\hosts /Y

Open in new window

Author

Commented:
Thank you for the quick responses. I will test the suggestions

Q
Most Valuable Expert 2011
Top Expert 2011
Commented:
Oh, just to clarify, mine was 2 different scripts... Decided to make it nice and pretty....  :)

And \\Server\share can be a local path as well, depending on your setup.

Just copy the hosts as Host1.txt with 1 value, and hosts2.txt as the other one.

I tested the errors, so the copy should work fine.
 
@echo off 
setlocal
cls
echo Choose which address you want in the HOSTS File: 
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo.
echo  a) 192.168.0.254
echo  b) 193.168.0.1
echo.

set /p ask=Select a or b now: 
echo.

if %ask%==a goto hosts1
if %ask%==b goto hosts2

:hosts1
copy /Y \\server\share\hosts1.txt c:\windows\system32\drivers\etc\hosts 
if not %errorlevel%==0 goto error
echo "SITENAMEHERE" has been set to 192.168.0.254 in the HOSTS file.
goto end

:hosts2
@echo off 
copy /Y \\server\share\hosts2.txt c:\windows\system32\drivers\etc\hosts
if not %errorlevel%==0 goto error
echo "SITENAMEHERE" has been set to 193.168.0.1 in the HOSTS file. 
goto end

:error
echo There was a problem, HOSTS file not changed.
echo.
pause

:end

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial