Append Text to a file

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
questfortruthAsked:
Who is Participating?
 
sirbountyCommented:
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
 
sirbountyCommented:
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
 
questfortruthAuthor Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
questfortruthAuthor Commented:
Sorry.... it looks like:
10.23.45.56      website1.com

10.12.13.14               website.com
0
 
da99rmdCommented:
It works fine for me.

/Rob
0
 
sirbountyCommented:
Do you mean the tab is too wide?  Try

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

instead...
0
 
questfortruthAuthor Commented:
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
 
sirbountyCommented:
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
 
RDAdamsCommented:
you could also go to the location and type Edit Host to bring up the default text editor in NT.
0
 
questfortruthAuthor Commented:
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
 
questfortruthAuthor Commented:
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
 
questfortruthAuthor Commented:
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
 
sirbountyCommented:
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
 
questfortruthAuthor Commented:
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
 
questfortruthAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.