Solved

batch file to replace string in the hosts file

Posted on 2007-11-14
6
1,471 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Have you considered what group policies are backwards and forwards compatible? Windows Active Directory servers and clients use group policy templates to deploy sets of policies within your domain. But, there is a catch to deploying policies. The…
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…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
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…

758 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

14 Experts available now in Live!

Get 1:1 Help Now