Find and Replace text
Moderator: DosItHelp
-
- Posts: 3
- Joined: 13 Sep 2010 22:26
Find and Replace text
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.
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.
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Find and Replace text
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.
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
-
- Posts: 3
- Joined: 13 Sep 2010 22:26
Re: Find and Replace text
Thanks a lot
Any way I can make it 'runnable' by non-admins?
Any way I can make it 'runnable' by non-admins?
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Find and Replace text
By changing permissions, unrelated to DOS, so without admin permissions, not likely.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Find and Replace text
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
-
- Posts: 319
- Joined: 12 May 2006 01:13
-
- Posts: 3
- Joined: 13 Sep 2010 22:26
Re: Find and Replace text
For me it works well, at least on the admin account.
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Find and Replace text
I tried it on your test file and it works fine. Maybe you could investigate.
Re: Find and Replace text
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%