Solved

Before running batch file how to detect the available entry in host file

Posted on 2010-11-09
6
753 Views
Last Modified: 2012-06-22
Hello Experts,

Need urgent help!

I want to update windows hosts file for certain entries like IP-Address & Host-name by a DOS batch script.

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 white-space
or tab-space which i don't know before hand. So while checking if the entry exists or not i just have to compare the strings ignoring the tab/white-space in between ip and host-name.

Valuable advices will be highly appreciated.

Thanks
0
Comment
Question by:newbie27
6 Comments
 
LVL 7

Expert Comment

by:TechnoChat
ID: 34099348
You can use following command to check a specific entry on host file..

more %windir%\system32\drivers\etc\hosts | findstr string

Eg.
Following command will output the current localhost details.
more %windir%\system32\drivers\etc\hosts | findstr localhost

Thanks
Saugata
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 250 total points
ID: 34100062
Personally I'd do it slightly different but same sort of tact. The /i deals with upper/lower case in findstr and you can use || to check if the errorlevel returned is 0 (i.e. not found) and then display the need to add and append it to the file.  Give it a try anyway:

Steve

@echo off
set hosts="%windir%\system32\drivers\etc\hosts"

call :check host1 1.1.1.1
call :check host2 1.1.1.2
call :check host3 1.1.1.3

exit /b

:check
REM Check if first parameter exists in hosts file.  If it doesn't then echo new value to end of file
findstr /i "%1" || (
  echo %1  %2 >>%hosts%
  echo Adding %1  %2 to HOSTS table
)

0
 
LVL 53

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 34101056
Keep in mind though that on Windows 7 and Vista the hosts file is typically protected, and you'll need admin access to update it.

Also, is it important that the exact match of hostname and IP address already be in the table, or as Steve has demonstrated, is looking only one okay?  The other thing Steve's code won't catch is if that line is commented out.

Steve, what do you think about using FINDSTR with a regex expression, looking for the hostname followed by the IP address with just white space between them?  In addition, maybe we could rig a regex that makes sure there isn't a semi colon at the left of those?

~bp
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 34101181
Good idea. I didn't have much time so went for the easy option, I suppose it depends what is likely to be in the table for this environment, and as you say many users may not be able to update / access HOSTS for writing anyway.

I'm busy at the mo. so go for it with the alternate regex version as you wish.

Steve
0
 
LVL 53

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 34101506
Okay, here's a change that would get closer.  The problem with this is that FINDSTR regex support doesn't include any way to look for tab characters.  So if that is really needed, we would need to use a more ribust GREP tool, like GNU GREP.  @newbie27, would that be acceptable?

@echo offset hosts="%windir%\system32\drivers\etc\hosts"call :check host1 1.1.1.1call :check host2 1.1.1.2call :check host3 1.1.1.3exit /b:checkREM Check if entry exists in hosts file.  If it doesn't then echo new value to end of filefindstr /i /r /c:"^ *%2  *%1 " || (  echo %1  %2 >>%hosts%  echo Adding %1  %2 to HOSTS table)
~bp
0
 
LVL 8

Author Comment

by:newbie27
ID: 34111888
ok thanks for your input folks, let me try and get back to you shortly !!
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many times while working on a computer regardless of any Operating System, lag and crashes seem to creep in, hindering your working speed. Sometimes, it can also cause your work to be lost unexpectedly and as a result, you are unable to meet your de…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question