Computing.Net > Forums > Programming > Updating hosts file with a Batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Updating hosts file with a Batch

Reply to Message Icon

Name: Patplays852
Date: April 25, 2009 at 19:31:24 Pacific
OS: Windows XP
CPU/Ram: 3GB
Subcategory: Batch
Comment:

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:
ftp://dan:(port)

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 =]



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: April 25, 2009 at 21:26:37 Pacific
Reply:

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.
)
popd


0

Response Number 2
Name: Patplays852
Date: April 25, 2009 at 22:10:36 Pacific
Reply:

works a little bit, but now it has this in the hosts file:

192.168.0.12 dan #dan's dynamic address.
141.149.26.216 dan #dan's dynamic address.

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


0

Response Number 3
Name: Judago
Date: April 25, 2009 at 22:20:54 Pacific
Reply:

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.
)
popd


0

Response Number 4
Name: Patplays852
Date: April 25, 2009 at 22:34:04 Pacific
Reply:

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.


0

Response Number 5
Name: Patplays852
Date: April 25, 2009 at 22:39:24 Pacific
Reply:

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!


0

Related Posts

See More



Response Number 6
Name: Judago
Date: April 25, 2009 at 22:40:41 Pacific
Reply:

(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")

[Edit]

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

[/edit]


0

Response Number 7
Name: Mechanix2Go
Date: April 26, 2009 at 03:45:26 Pacific
Reply:

@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.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Updating hosts file with a Batch

Editing a Text File With a Batch www.computing.net/answers/programming/editing-a-text-file-with-a-batch/14889.html

Editing .ini file with a batch file www.computing.net/answers/programming/editing-ini-file-with-a-batch-file/12127.html

moving files with a .bat www.computing.net/answers/programming/moving-files-with-a-bat/17109.html