Solved

Append Text to a file

Posted on 2004-04-19
15
3,343 Views
Last Modified: 2013-11-18
Hi All,
I am using Win NT and am trying to make a batch file which can append a a phrase to a file in my computer.

I need to append the line
10.12.13.14 <tab> website.com
to the end of a file called "hosts" (does not have any file extention), present in the folder "C:\WINNT\System32\drivers\etc\"

Also, if the above mentioned phrase already exixts in the file, the batch file should give a message to the user and exit automatically without making any changes.

I really dont have any experience in doing this. Please can someone help me with this.

Thanks in advance,
questfortruth
0
Comment
Question by:questfortruth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 10858407
This should do it...

@echo off
findstr "10.12.13.14      website.com" %systemroot%\system32\drivers\etc\hosts
if %errorlevel%==0 goto msg
echo 10.12.13.14      website.com >> %systemroot%\system32\drivers\etc\hosts
goto :eof
:msg
cls
echo. This entry is already present in your HOSTS file

0
 

Author Comment

by:questfortruth
ID: 10858510
Hi sirbounty,
Thanks for the help. It works fine just one glitch.

The string is appended to the end of the file alright, but a blank line is also present, which is unwanted. e.g. is previously the last lime in the Hosts file is:
10.23.45.56      website1.com

then after the batch file is run, the file looks like:
10.23.45.56      website1.com
10.12.13.14     website.com

Please can you have a look.
Thanks in advance,
questfortruth
0
 

Author Comment

by:questfortruth
ID: 10858521
Sorry.... it looks like:
10.23.45.56      website1.com

10.12.13.14               website.com
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:da99rmd
ID: 10858757
It works fine for me.

/Rob
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10859048
Do you mean the tab is too wide?  Try

 echo 10.12.13.14    website.com >> %systemroot%\system32\drivers\etc\hosts

instead...
0
 

Author Comment

by:questfortruth
ID: 10860070
Hi sirbounty,
The tab space is not the problem. The problem that I am facing is that there seems to be an unnecessary blank line when the batch file makes the changes to the HOSTS file.

Thus, after the batch file is executed, the HOSTS file looks something like:
10.10.10.10    website1.com            'This is the last line in HOSTS file
                                                     'Unnecessary line break occurs in the file
10.12.13.14    website2.com            'This is the line added by the batch file

I dont want this unnecessary line space to occur in the HOSTS file.
I guess this occurs because in the HOSTS file, the cursor is already in the next line. When the batch file runs, it first adds another line break to it and then adds the phrase "10.12.13.14    website.com".

Hope I was clear enough. Please let me know if you need some more information. Thanks for all your help.
Himanshu
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10860887
Okay, I see what you're saying now - but the default HOSTS file shouldn't do this.
Was this file manually editted before and perhaps an extra space was placed there?

I can think of a couple of ways of resolving this, but need to know - do the HOSTS file(s) all include the preliminary comment lines?

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
etc, etc etc...

Or are they just the addresses you need there?
0
 
LVL 17

Expert Comment

by:RDAdams
ID: 10863406
you could also go to the location and type Edit Host to bring up the default text editor in NT.
0
 

Author Comment

by:questfortruth
ID: 10867409
Hi Sirbounty,
Thanks for the message. You got it ... the HOSTS file in my system has exactly this format. Meaning, it has the copyright notice in the beginning ... and then gives the listing of the addresses.

Can the batch file take care of both the cases... meaning the default format of the HOSTS file and also the manually edited format where the cursor is already in the next line?

The extra line break does not cause any problems in my HOSTS file, and Internet Explorer seems to be working fine. Do you know of any potential problems that may be caused by this. If no, then i guess, i wont worry about the line break.
Thanks,
questfortruth
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 50 total points
ID: 10867613
A) No problems with the space.  It won't impact your HOSTS at all.
B) If it bothers you though... (this will only work for 'original' hosts files with the comment lines (which most should be)


@echo off
set hostSrc=%systemroot%\system32\drivers\etc
findstr "10.12.13.14     website.com" %hostSrc%\hosts
if %errorlevel%==0 goto msg
ren %hostSrc%\hosts hosts.org
for /f "tokens=* skip=17" %%a in (%hostSrc%\hosts.org) do echo %%a >> %hostSrc%\hosts
echo 10.12.13.14     website.com >> %hostSrc%\hosts
goto :eof
:msg
cls
echo. This entry is already present in your HOSTS file
0
 

Author Comment

by:questfortruth
ID: 10868170
Sirbounty... Thanks a million for the help. Full points to you for the help and also for customizing it to such an extent.

Also, thanks for bearing with me and my repeated postings....:)

Thanks again,
questfortruth
0
 

Author Comment

by:questfortruth
ID: 10869785
Hi Sirbounty,
Its me again ... bumped into another problem. Presently i have made the batch file to work on Win NT systems. In WIN NT systems, the batch file goes to the C:\WINNT\System32\drivers\etc\hosts folder and then updates the hosts file.

However, in the case of WIN XP systems the HOSTS file located at C:\Windows\System32\drivers\etc\hosts

Can we change the batch file to accomodate that variation, so that the batch file can recognize the operating system and then update the HOSTS file accordingly. Thus when the Batch file senses a Win NT system, it uses the HOSTS file at the C:\WINNT\System32\drivers\etc\hosts folder and in the case of Win XP system, it uses the C:\Windows\System32\drivers\etc\hosts folder.

Ths code i am using presently is:
**************
@echo off
findstr "10.12.13.14      website1.com" %systemroot%\system32\drivers\etc\hosts
if %errorlevel%==0 goto msg
echo 10.12.13.14      website1.com >> %systemroot%\system32\drivers\etc\hosts
goto :eof
:msg
cls
echo. The Host file is already up-to-date!
**************
Thanks in advance.....
questfortruth
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10872320
My last code modification already took care of that problem... :)
Use the code from the accepted solution.
%systemroot% defaults to either/or depending on what OS is installed....
(I was being proactive - nice, eh? LOL)
0
 

Author Comment

by:questfortruth
ID: 10875541
Hi Sirbounty,
Thanks for the reply. I tried using the code as given below. And it works wonderfully.... :)

*************
@echo off
findstr "10.12.13.14     website1.com" %systemroot%\system32\drivers\etc\hosts
if %errorlevel%==0 goto msg
echo 10.12.13.14     website1.com >> %systemroot%\system32\drivers\etc\hosts
goto :eof
:msg
cls
echo. The Host file is already up-to-date!
**************

Smart coding.. :)
Thanks a lot again,
questfortruth
0
 

Author Comment

by:questfortruth
ID: 11359138
Hi Sirbounty,
I just needed some changes to the earlier code. Please can you help me with this?

What i need to do now is:
1. Scan for any existing references to 10.12.13.14
2. If the reference is present, ensure that it leads to WEBSITE1.COM
3. If the reference is not present, add this reference to the end of the file

Perform similar check for another reference 10.12.13.15 and ensure it leads to WEBSITE2.COM

Please can you help. Thanks,
questfortruth
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 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