Link to home
Start Free TrialLog in
Avatar of questfortruth
questfortruth

asked on

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
Avatar of sirbounty
sirbounty
Flag of United States of America image

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

Avatar of questfortruth
questfortruth

ASKER

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

10.12.13.14               website.com
It works fine for me.

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

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

instead...
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
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?
you could also go to the location and type Edit Host to bring up the default text editor in NT.
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
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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)
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
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