Clicky

I am trying to write a batch script that will add a line to the hosts file on a Windows XP professional machine. I have a rather simple script that I wrote as follows:

@echo off
set hostspath=%windir%\System32\drivers\etc\hosts
echo new entry  >> %hostspath%
exit

Obviously, I want the new entry to be on a new line. I ran the script on some test machines. On some, it worked correctly and placed the entry on a new line. On others however, it appened the entry to the last line of the file. I'm not sure why it's doing this, unless it has something to do with where the cursor was the last time the file was saved. I plan on pushing this out to a lot of machines, and I don't want to break the hosts file on any of them. Is there a simple way I can modify my file so that it starts a new line no matter what? Thank you

asked 12/14/2011 05:00

rsturtevant's gravatar image

rsturtevant ♦♦


4 Answers:
Add a blank line first:

1:
2:
3:
4:
5:
@echo off
set hostspath=%windir%System32driversetchosts
echo >> %hostspath%
echo new entry  >> %hostspath%
exit
link

answered

sjklein42's gravatar image

sjklein42

1:
2:
3:
4:
5:
@echo off
set hostspath=%windir%System32driversetchosts
echo. >>  %hostspath%
echo new entry  >> %hostspath%
exit
link

answered 2011-12-14 at 13:09:08

brutaldev's gravatar image

brutaldev

Part of this question has been answered before, see here.

To get an empty line, you have to do this:



because echo >> %hostspath%

will add "echo on" to the hosts file.
link

answered 2011-12-14 at 13:11:58

gerwinjansen's gravatar image

gerwinjansen

Thanks guys
link

answered 2011-12-14 at 13:19:41

rsturtevant's gravatar image

rsturtevant

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×11
×1

Asked: 12/14/2011 05:00

Seen: 180 times

Last updated: 12/14/2011 05:21