Using Windows Batch File to replace Hosts file

Hello,

I currently have a logon script that is set to replace the local machine Hosts file with that of a modified hosts file.

The problem that I am running into is that the original hosts file on the local machine is read-only, so the file is not copied over. I figured that out after removing the read-only access and then running my script again.

I am looking for the code snippet to remove the "read-only" permission before the script attempts to copy the file.

Can anyone show what the line of code I need for this?
copy "\\server\share\Hosts\*.*" "%systemroot%\system32\drivers\etc\" /Y

Open in new window

id4382Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You can use xcopy instead, with option /R:

xcopy /R /Y /D \\server\share\Hosts\*.*" "%systemroot%\system32\drivers\etc\"
Option /D can be omitted, it will copy only files newer than the destination.
0
optomaCommented:
Is there entries already in the hosts file apart from default entry?
0
Bartender_1Microsoft Network AdministratorCommented:
the command you're looking for in your script is the attrib command. Add this to your script:

attrib -r %%systemroot%\system32\drivers\etc\hosts"


Hope this helps!

:o)

Bartender_1
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
When you use attrib, then copy, you should reset the r/o flag, so HOSTS is write-protected again:

attrib -r "%systemroot%\system32\drivers\etc\hosts"
copy "\\server\share\Hosts\*.*" "%systemroot%\system32\drivers\etc\" /Y
attrib +r "%systemroot%\system32\drivers\etc\hosts"
xcopy can do that in one go, if the source file is r/o:
xcopy /R /Y /D /K \\server\share\Hosts\*.*"  "%systemroot%\system32\drivers\etc\"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
id4382Author Commented:
The commands given by Qlemo worked successfully.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows XP

From novice to tech pro — start learning today.