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

Batch file for hosts updation

Reply to Message Icon

Original Message
Name: arin123
Date: June 9, 2008 at 09:05:04 Pacific
Subject: Batch file for hosts updation
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


Report Offensive Message For Removal

Response Number 1
Name: Mechanix2Go
Date: June 10, 2008 at 01:27:27 Pacific
Subject: Batch file for hosts updation
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 2
Name: arin123
Date: June 10, 2008 at 03:24:49 Pacific
Subject: Batch file for hosts updation
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 3
Name: Mechanix2Go
Date: June 10, 2008 at 05:14:13 Pacific
Subject: Batch file for hosts updation
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 4
Name: arin123
Date: June 10, 2008 at 08:17:05 Pacific
Subject: Batch file for hosts updation
Reply: (edit)

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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch file for hosts updation

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software