Solved

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

Posted on 2006-10-30
10
4,321 Views
Last Modified: 2007-12-19
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!!
0
Comment
Question by:Caiapfas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 2

Author Comment

by:Caiapfas
ID: 17836613
maybe a small .exe
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 17836675
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<---------------------------------

0
 
LVL 4

Expert Comment

by:mctigue
ID: 17889238
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)
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:mctigue
ID: 17889318
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
0
 
LVL 4

Expert Comment

by:mctigue
ID: 17925963
did that work?
0
 
LVL 4

Expert Comment

by:mctigue
ID: 17968458
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

0
 
LVL 4

Accepted Solution

by:
mctigue earned 500 total points
ID: 17968520
@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





just fixed some capitalization errors.  that should work.  Good luck.
0
 
LVL 2

Author Comment

by:Caiapfas
ID: 18097480
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
0
 
LVL 2

Author Comment

by:Caiapfas
ID: 18105721
PLEASE help it's very urgent!!!
0
 
LVL 2

Author Comment

by:Caiapfas
ID: 18119843
500 more points here, please help me with the last part!!
http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_22089973.html
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question