Find and Replace text

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
LeppeRMessiaH
Posts: 3
Joined: 13 Sep 2010 22:26

Find and Replace text

#1 Post by LeppeRMessiaH » 13 Sep 2010 22:37

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.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Find and Replace text

#2 Post by orange_batch » 13 Sep 2010 23:03

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

LeppeRMessiaH
Posts: 3
Joined: 13 Sep 2010 22:26

Re: Find and Replace text

#3 Post by LeppeRMessiaH » 13 Sep 2010 23:18

Thanks a lot
Any way I can make it 'runnable' by non-admins?

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Find and Replace text

#4 Post by orange_batch » 14 Sep 2010 00:14

By changing permissions, unrelated to DOS, so without admin permissions, not likely.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Find and Replace text

#5 Post by ghostmachine4 » 14 Sep 2010 01:23

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


ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Find and Replace text

#6 Post by ghostmachine4 » 14 Sep 2010 01:28

@orange, tried your code, got

Code: Select all

find: invalid predicate `'

LeppeRMessiaH
Posts: 3
Joined: 13 Sep 2010 22:26

Re: Find and Replace text

#7 Post by LeppeRMessiaH » 14 Sep 2010 01:49

For me it works well, at least on the admin account.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Find and Replace text

#8 Post by orange_batch » 14 Sep 2010 03:30

ghostmachine4 wrote:@orange, tried your code, got

Code: Select all

find: invalid predicate `'


I tried it on your test file and it works fine. Maybe you could investigate.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Find and Replace text

#9 Post by amel27 » 16 Sep 2010 00:58

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%

Post Reply