Basically I want to edit my hosts file and change some of the ip addresses in it. Like the hosts files have:
127.0.0.1 localhost
102.54.94.97 rhino1.acme.com
102.54.94.97 rhino2.acme.com
102.54.94.97 rhino3.acme.com
And I want to change it to
127.0.0.1 localhost
102.54.94.3 rhino1.acme.com
102.54.94.3 rhino2.acme.com
102.54.94.3 rhino3.acme.com
Here's what I've got so far but it isnt really working well. The first part finds any instances of acme.com and deletes the entire line. The second part prompts for the new port number and uses it in the last part to add the lines back into host. The deleting and renaming back to hosts file works fine :S but that's about it lol
Actually it works fairly well but eveytimeI run bat file again, it adds another blank line because of the echo. and I'm not sure how to remove blank lines in the hosts file. The reason I put in the echo. is because if it isn't there the new lines just add to the end of the localhost line and not a new line. So I can't do without it, but with it, it just keeps adding blank lines each time I run it lol.
Any help in fixing or rewriting this would be appreciated. It's a fun way to learn this but I'm out of ideas what to google on this lol.
Code: Select all
@echo off
setlocal enabledelayedexpansion
findstr/c:"acme.com" /i /v C:\WINDOWS\system32\drivers\etc\hosts > C:\WINDOWS\system32\drivers\etc\hosts1
:PICK
CLS
Echo "Enter Port Number (1,2,3,4)
set /p PORT=
If %PORT%==1 GOTO EDIT
If %PORT%==2 GOTO EDIT
If %PORT%==3 GOTO EDIT
If %PORT%==4 GOTO EDIT
CLS
Echo "The PORT you picked does not exist, try again"
PAUSE
GOTO PICK
:EDIT
ECHO.>>C:\WINDOWS\system32\drivers\etc\hosts1
ECHO 121.128.133.%PORT% rhino1.acme.com>>C:\WINDOWS\system32\drivers\etc\hosts1
ECHO 121.128.133.%PORT% rhino2.acme.com>>C:\WINDOWS\system32\drivers\etc\hosts1
ECHO 121.128.133.%PORT% rhino3.acme.com>>C:\WINDOWS\system32\drivers\etc\hosts1
del C:\WINDOWS\system32\drivers\etc\hosts
ren C:\WINDOWS\system32\drivers\etc\hosts1 C:\WINDOWS\system32\drivers\etc\hosts