• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1484
  • Last Modified:

batch file to replace string in the hosts file

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
anternieTS
Asked:
anternieTS
  • 3
1 Solution
 
512ThzCommented:
@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
 
512ThzCommented:
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
 
JameshontisharCommented:
Are you using this in a domani, if so use a batch file associated to a GPO
0
 
512ThzCommented:
I think I answered the question. Please accept it or tell us what's wrong
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now