Computing.Net > Forums > Disk Operating System > Batch file for hosts updation

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file for hosts updation

Reply to Message Icon

Name: arin123
Date: June 9, 2008 at 09:05:04 Pacific
OS: Win XP
CPU/Ram: 1 GB
Comment:

I want to update windows hosts file for certain entries by a DOS batch script.
The entries i want to add are written in a text file data.taxt as below:

10.10.10.10 xxx.abc.com
10.10.10.10 yyy.abc.com
10.10.10.10 zzz.abc.com

But before adding these entries i want to check if any entry already exists in
hosts file or not. If exists, it will not be added.

In hosts file, existing entries i.e. ip and hostname may be separated by whitespace
or tabspace which i don't know before hand. But in data.txt file i have the entries to be added separated by whitespace only.

I am facing problem while checking if the entry exists or not as i have to
compare the strings ignoring the tab/whitespace in between ip&hostname.

My batch file is as follows(not in running condition):
@echo off
setlocal enabledelayedexpansion

set HOSTS =%windir%\system32\drivers\etc\hosts

FOR /F "delims=~" %%a in (data.txt) DO
(
set var=%%a
set var=!var: =! ///FOR whitespace removal
set var=!var: =! /// FOR tabspace removal
FOR /F "delims=^n tokens=*" %%b in (%HOSTS%) DO
(
(IF "!var!"=="%%b" SET FOUND=TRUE)
IF %FOUND%.==TRUE. (ECHO !var! is present in HOSTS) ELSE (ECHO !var! >> %HOSTS%)
)
)

arin



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 10, 2008 at 01:27:27 Pacific
Reply:

In NT:

@echo off
setLocal EnableDelayedExpansion

pushd C:\WINNT\system32\drivers\etc

for /f "tokens=1-2 delims= " %%a in (data.txt) do (
find /i "%%b" < hosts > nul
if errorlevel 1 echo %%a %%b >> hosts
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: arin123
Date: June 10, 2008 at 03:24:49 Pacific
Reply:

Thanks. This script is working fine but it fails if in hosts there is entry like uat-xxx.abc.com. because of this it will not add
xxx.abc.com as it finds xxx.abc.com in uat-xxx.abc.com. I want to compare the ip&hostname as one string ignoring tab/whitespace.

arin


0

Response Number 3
Name: Mechanix2Go
Date: June 10, 2008 at 05:14:13 Pacific
Reply:

You're right. I should never work on a script before I have my morning coffee. LOL

I'll work on it.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: arin123
Date: June 10, 2008 at 08:17:05 Pacific
Reply:

thanks for your reply,. I have modified the script and it is working fine except that if ip&hostname in hosts file are not separated by tab/whitespace instead of say two number of whitespace characters or any number of whitespace character.

script is below:
@echo off
setLocal EnableDelayedExpansion

SET HOSTS==%windir%\system32\drivers\etc\hosts

for /f "tokens=1-2 delims= " %%a in (finddata.txt) do (
find /i "%%a--whitespace-- %%b" < %HOSTS% > nul (ip&hostname separated by whitespace)
|| find /i "%%a--tab--%%b" < %HOSTS% > nul ((ip&hostname separated by tab)
if errorlevel 1 echo %%a %%b >> %HOSTS%
if errorlevel 0 echo %%a %%b already exists in hosts
)
pause
echo on

----------
Please help

arin


0

Response Number 5
Name: Honeyf
Date: September 7, 2008 at 23:31:21 Pacific
Reply:

Hi Friends,

I need a simple batch file to just an entry in the host file. I dont want to check whether the IP already exist or not. I want to pass the entry values in the batch file itself? can some one help me?

-Honeyf


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

config.sys devices IBM Thinkpad i1400 2611-4...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Batch file for hosts updation

Can you execute a batch file for a web page www.computing.net/answers/dos/can-you-execute-a-batch-file-for-a-web-page-/5382.html

Pause a batch file for 10 sec' www.computing.net/answers/dos/pause-a-batch-file-for-10-sec/8249.html

batch files for link analysis www.computing.net/answers/dos/batch-files-for-link-analysis/14281.html