Updating hosts file with a Batch

April 25, 2009 at 19:31:24
Specs: Windows XP, 3GB
Hello, I've tried to find what i was looking for on the forums and the internet and have not been able to find an answer yet.

Here is the situation:

I set up an ftp server on my friends computer (yippie) the bad news is his ISP gives him a dynamic EXTERNAL IP address... bad news for ftp'ing lol. We found a program that sences when his external ip changes, and it uploads a text file to my ftp server with just the new IP in it.

I would like to make a batch file that runs every hour or so (i can do that) but i want it to DELETE the old entry in the hosts file and then add the new ip to the hosts file. (I have the text file setup so that it has:
***.***.***.*** [tab] dan

this way when i want to ftp into his server all i need to do is type:

The key part that is stopping me from acomplishing this is that i need the old entry to be deleted, otherwise i am going to have multiple addresses for the same host (dan)

Thank you very much for your help =]

See More: Updating hosts file with a Batch

Report •

April 25, 2009 at 21:26:37
What about using a unique comment to sift out the entry since it is
always going to be scripted? It would make things very easy by using find.

***.***.***.*** [tab] dan #dan's dynamic address.

pushd "%systemroot%\system32\drivers\etc"
type hosts|find /i /v "#dan's dynamic address." > hosts.new
move /y host.new hosts
for /f "usebackq" %%a in ("drive:\path\iptextfile") do (
>>hosts echo %%a 	 dan #dan's dynamic address.

Report •

April 25, 2009 at 22:10:36
works a little bit, but now it has this in the hosts file: dan #dan's dynamic address. dan #dan's dynamic address.

so now how would i get it to delete the old stuff?

Report •

April 25, 2009 at 22:20:54
Sorry the move command didn't work the same in a script as on the commandline:

pushd "%systemroot%\system32\drivers\etc"
type hosts|find /i /v "#dan's dynamic address" > hosts.new
move "%cd%\hosts.new" "%cd%\hosts"
for /f "usebackq" %%a in ("drive:\path\textfile") do (
>>hosts echo %%a 	 dan #dan's dynamic address.

Report •

Related Solutions

April 25, 2009 at 22:34:04
ok, it deletes the old setting perfectly now, now the only problem is i get this error:

C:\WINDOWS\system32\drivers\etc>for /F "usebackq" %a in (D:\FROM DAN\IP\DansIP.txt") do (echo %a dan #dan's dynamic address. 1>>hosts )
The system cannot find the file D:\FROM DAN\IP\DansIP.txt.

I double checked, and the file is there.

Report •

April 25, 2009 at 22:39:24
Sweet, i got it to work!

(i had the address typed wrong, i need an underscore "_" for a space in the FROM_DAN!!!!! lol

thank you very much for your help, batch works like a charm!

Report •

April 25, 2009 at 22:40:41
(D:\FROM DAN\IP\DansIP.txt")

Does it really only have one double quote? The whole path name should be surrounded by double quotes.

("D:\FROM DAN\IP\DansIP.txt")


Beat me to it........


Report •

April 26, 2009 at 03:45:26
@echo off > newfile & setLocal EnableDelayedExpansion

set /p new=<dansIP.txt

for /f "tokens=-1-2 delims=" %%a in (hosts) do (
if %%b equ dan (
echo !new! %%b >> newfile
) else (
echo %%a %%b >> newfile
copy newfile hosts

If at first you don't succeed, you're about average.


Report •

Ask Question