Solved

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

Posted on 2006-10-30
10
4,313 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

785 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