Solved

Append Text to a file

Posted on 2004-04-19
15
3,340 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
15 Comments
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry.... it looks like:
10.23.45.56      website1.com

10.12.13.14               website.com
0
 
LVL 8

Expert Comment

by:da99rmd
Comment Utility
It works fine for me.

/Rob
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:RDAdams
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now