Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.comBut 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 enabledelayedexpansionset 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

In NT:
@echo off
setLocal EnableDelayedExpansionpushd 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

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

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

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 EnableDelayedExpansionSET 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 helparin

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

![]() |
config.sys devices
|
IBM Thinkpad i1400 2611-4...
|

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