Page 1 of 1
Find and Replace text
Posted: 13 Sep 2010 22:37
by LeppeRMessiaH
Hello,
I wonder if anyone can help me with a script that searches and replaces or adds (if it doesn't find) a line of text in Hosts file.
I did a menu already but I ran into this and I can't seem to successfully do it.
I need it to search for "link.company.com" for example and replace the ip in front of it or add the ip along with link.company.com
Thanks in advance, I hope I made myself clear.
Re: Find and Replace text
Posted: 13 Sep 2010 23:03
by orange_batch
Here's a solution.
Replaces a line containing "findhost" with "prepend findhost". If not found, appends it to hostsfile.
With setlocal and endlocal, this is safe to insert into a script.
Code: Select all
setlocal enableextensions enabledelayedexpansion
setlocal
:: Options:
set "hostsfile=C:\...\hostsfile"
set "findhost=link.company.com"
set "prepend=123.123.123.123"
set found=
for /f "delims=] tokens=1*" %%x in ('type "!hostsfile!"^|find /v /n ""^&type nul^>"!hostsfile!"') do (
set toggle=
for /f "delims=" %%z in ('echo:"%%y"^|find "!findhost!"') do (
set toggle=1
set found=1
echo:!prepend! !findhost!>>"!hostsfile!"
)
if not defined toggle echo:%%y>>"!hostsfile!"
)
if not defined found echo:!prepend! !findhost!>>"!hostsfile!"
endlocal
Re: Find and Replace text
Posted: 13 Sep 2010 23:18
by LeppeRMessiaH
Thanks a lot
Any way I can make it 'runnable' by non-admins?
Re: Find and Replace text
Posted: 14 Sep 2010 00:14
by orange_batch
By changing permissions, unrelated to DOS, so without admin permissions, not likely.
Re: Find and Replace text
Posted: 14 Sep 2010 01:23
by ghostmachine4
download
gawk for windows, then use this
Code: Select all
C:\test>more file
# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
127.0.0.1 localhost
x.x.x.x link.com
C:\test>gawk "/link\.com/{f=1;$1=\"10.10.10.10\";}{a[++c]=$0}END{for(i=1;i<=c;i++) print a[i];if(!f)print \"10.10.10.10 link.com\"}" file
# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
127.0.0.1 localhost
10.10.10.10 link.com
Re: Find and Replace text
Posted: 14 Sep 2010 01:28
by ghostmachine4
@orange, tried your code, got
Re: Find and Replace text
Posted: 14 Sep 2010 01:49
by LeppeRMessiaH
For me it works well, at least on the admin account.
Re: Find and Replace text
Posted: 14 Sep 2010 03:30
by orange_batch
ghostmachine4 wrote:@orange, tried your code, got
I tried it on your test file and it works fine. Maybe you could investigate.
Re: Find and Replace text
Posted: 16 Sep 2010 00:58
by amel27
Code: Select all
set find=link.company.com
set addr=123.123.123.123
call set host=%windir%\system32\drivers\etc\hosts
findstr /iec:"%find%" "%host%" >nul&&(
findstr /viec:"%find%" "%host%" >"%~n0.tmp"
copy /y "%~n0.tmp" "%host%")
>>"%host%" echo %addr% %find%