Batch File creation - Help Needed.

Status
Not open for further replies.

Bladerunner2019

Solid State Member
Messages
7
Hi

Ive created a cd that autoruns a graphical menu with clickable links, and one of the links points to a HOSTS.bat file that copies over a custom HOSTS file from the cd to the ..\drivers\etc folder on the system root.

The problem I'm having is that I'm unsure of the correct syntax to use to automatically overwrite the existing HOSTS file.

I need to write it to work on both Win2K and XP machines, so have included two lines; one copies the HOSTS file to WINDOWS\system32\drivers\etc, and one copies it to Winnt..\etc. Thsi seems to work ok, for if it's an XP machine it ignores the Winnt instruction and vica versa.

However, this has all been tested with my original HOSTS file temporarily renamed to HOSTS2, so there is no HOSTS file to be overwritten. I cannot seem to make it work if there is a HOST file already present.

Is there a switch to make it overwrite? Or changing tack entirely, is there a way I can instruct the IP address I need to be written into the original HOSTS file?

Here is the contents of the batch file as it stands:

rem

copy "hosts" "C:\WINDOWS\system32\drivers\etc\hosts"

copy "hosts" "C:\WINNT\system32\drivers\etc\hosts"

If anyone can help I would be most appreciative!

TIA ;-)
 
What error does it respond with when you try to replace the host file?

put a "pause" at the end of your batch file so you have time to read the error message.

Are you going to be using this under users with administrator access, power user or user access?

Under the administrator account it works perfectly to just use the copy command.

If it's prompting for confirmation of the replacement, you can do the following

Code:
 copy -y location destination
 
I'm headed home from work, where I use my mac, so here's a copy of the batch file that I would use.

Code:
@echo off

copy /y hosts c:\windows\system32\drivers\etc\
copy /y hosts c:\winnt\system32\drivers\etc\

If you wanted to make it a bit more fool proof, I'd check if Winnt exists vs Windows.

Code:
@echo off

if exist c:\winnt\ (
copy /y hosts c:\winnt\system32\drivers\etc
) else (
copy /y hosts c:\windows\system32\drivers\etc
)

Lastly, if that still doesn't work on your system, you could always rename the file, then copy the new one.

Code:
@echo off

if exist c:\winnt\ (
rename c:\winnt\system32\drivers\etc\hosts hosts.bak
copy hosts c:\winnt\system32\drivers\etc\
) else (
rename c:\windows\system32\drivers\etc\hosts hosts.bak
copy hosts c:\windows\system32\drivers\etc\
)
 
Solved!!

Thanks for the response bla!!
However, I've now solved it - although I believe your suggestion would indeed work, I've discovered the only code I need is the following:

rem

copy /y HOSTS "%windir%\system32\drivers\etc\HOSTS"

This gets around the problem of there being differently named windows system folders.

Thanks again bla!!

Bladerunner2019

;-)
 
Status
Not open for further replies.
Back
Top Bottom