Solved

batch file to replace string in the hosts file

Posted on 2007-11-14
6
1,472 Views
Last Modified: 2012-06-27
I need to create a batch file that will find and replace a sting in the hosts file on nt workstation 4.0.  For example, I want the batch file to find the line :-

server1    182.168.1.2

and replace it with

server1   182.168.1.34

I am used to writing vbs scripts but don't want to use vbs as I would need to install the run time engine.  Is there a way of doing it cmd batch files?
0
Comment
Question by:anternieTS
  • 3
6 Comments
 
LVL 4

Expert Comment

by:512Thz
ID: 20285826
@echo off
find /V "192.168.1.2" < lmhosts > temp.tmp
echo server1 192.168.1.34 >> temp.tmp
ren lmhosts *.bak
ren temp.tmp lmhosts
0
 
LVL 4

Accepted Solution

by:
512Thz earned 250 total points
ID: 20289352
A better solution (it sends the "server1" entry on top of file (which is recommended)
NOTE. the file might be named "hosts" instead of "lmhosts"
_______________________
@echo off
if exist hosts.bak del hosts.bak
ren hosts hosts.bak
echo server1 192.168.1.34 > hosts
find /V "192.168.1.2" < hosts.bak >> hosts
_______________________

btw You normally do'nt have to install a run time to execute a VBS what OS are you running?
0
 

Expert Comment

by:Jameshontishar
ID: 20314819
Are you using this in a domani, if so use a batch file associated to a GPO
0
 
LVL 4

Expert Comment

by:512Thz
ID: 20330751
I think I answered the question. Please accept it or tell us what's wrong
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20521562
Forced accept.

Computer101
EE Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now