Link to home
Start Free TrialLog in
Avatar of Caiapfas
Caiapfas

asked on

need a batch file to add a line of text to the host file

I'm a web developer and have to have each client edit their host file so they can see their website BEFORE they change their dns. Having to walk them through it each time is insane. I can't send them through the ip for our site catchs that and sends them to the domian , sending them back to the old website.


So what I need is a batch file that once clicked on from the desktop opens and ask for ip (enter here) hit enter, then domain name (enter here) hit enter. Then takes that info and enters it at the end of their hosts file.

And then away for this same file to remove the entry after we change their dns would be nice!!
Avatar of Caiapfas
Caiapfas

ASKER

maybe a small .exe
Avatar of Lee W, MVP
TEST THIS FIRST.  It should work, but I'm most uncertain about the "restore" part.  Also, if the user does not have admin rights, this won't work.

---------------8<---------------------------------
@echo off
set /p SiteLive=Is the site live? (y/N):
if "%sitelive%"=="Y" Goto RestoreHosts
set /p IP=Enter IP Address:
set /p DomName=Enter Domain Name:
copy "%windir%\system32\drivers\etc\hosts" "%windir%\system32\drivers\etc\hosts.%date:~-4%%date:~4,2%%date:~7,2%.live"
Echo %ip%   %domname%>>"%windir%\system32\drivers\etc\hosts"
Goto End
:RestoreHosts
REM Backup current hosts file
copy "%windir%\system32\drivers\etc\hosts" "%windir%\system32\drivers\etc\hosts.%date:~-4%%date:~4,2%%date:~7,2%.dev"
REM Restore previously backed up hosts file
copy "%windir%\system32\drivers\etc\hosts.*.live" "%windir%\system32\drivers\etc\hosts"
:End
---------------8<---------------------------------

try this.  I don't know where the host file would be located, but just put the path where the parentheses are right now. for example if the host file was called hostfile.txt and was in the windows directory, delete the parentheses and everything between them, and put: c:\windows\hostfile.txt
of course that isn't really where it is or what it is called, but you get the idea.



@echo off
set /p ip=Enter ip here:
set /p domain=Enter domain here:
echo %ip%>>(the name & path of host file)
echo %domain%>>(the name & path of host file)
if you want it to look a little more fancy, you could also do this:


@echo off
set /p ip=Enter ip here:
echo %ip%>>(the name & path of host file)
echo IP copied to host file
pause
cls
set /p domain=Enter domain here:
echo %domain%>>(the name & path of host file)
echo Domain copied to host file
pause
cls
echo Done!
pause
did that work?
actually, that won't work because that will put the ip and domain at separate lines of the host file.  here is what will work:


@echo off
set /p ip=Enter IP here:
set /p Domain=Enter domain here:
echo %ip%    %domain%>>%systemroot%\System32\drivers\etc\HOSTS
cls
echo Done!
pause






and if you want to be able to check the host file and see if it was copied correctly, you could add this line to the bottom:

notepad %systemroot%\System32\drivers\etc\HOSTS

ASKER CERTIFIED SOLUTION
Avatar of Stephen McTigue
Stephen McTigue
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it worked GREAT mctigue , but it needs to put it at the VERY end on a new line. it just put it after another entry i had, NICE work
PLEASE help it's very urgent!!!