1.

Solve : .bat to update hostfile?

Answer»

I need help with making a .bat to download a .zip file from http://winhelp2002.mvps.org/hosts.zip and extract and replace the file host file in C:\Windows\System32\drivers\etc automatically. I have searched researched and TRIED but cannot get to work.

thanks
Neil Unfortunately this can be used for malicious intent, so i DOUBT anyone will help with how to make a batch file that downloads from a URL that can be any good or evil URL, then zip file extracting and copying a host file overtop of someone's system which may not be your own. There are scripts to download files at many places on the net and so this task shouldn't be considered a problem.

To unzip the file you will need a third party tool, and to replace HOSTS you will have to provide admin permissions.

This batch file and associated VBS script will download a ZIP file.

Code: [Select]@echo off
start geturl.vbs http://winhelp2002.mvps.org/hosts.zip hosts.zip

:: GETURL.VBS
Code: [Select]Set objArgs = WScript.Arguments
url = objArgs(0)
pix = objArgs(1)
With CreateObject("MSXML2.XMLHTTP")
.open "GET", url, False
.send
a = .ResponseBody
End With
With CreateObject("ADODB.Stream")
.Type = 1 'adTypeBinary
.Mode = 3 'adModeReadWrite
.Open
.Write a
.SaveToFile pix, 2 'adSaveCreateOverwrite
.Close
End With
You can save yourself some time and the BOTHER of a third party zip tool by downloading the text version of the HOSTS file.

Code: [Select]
$client = New-Object System.Net.WebClient

$url = "http://www.mvps.org/winhelp2002/hosts.txt"
$hosts = "c:\windows\system32\drivers\etc\hosts"
$path = "C:\windows\system32\drivers\etc"

if (Test-Path $hosts) {
$file = Get-Item -Path $hosts -Force
$file.Attributes = 'Normal'
}

$client.DownloadFile($url, $hosts)
$file.Attributes = 'ReadOnly'

Your OS comes with Powershell installed, so you may as well take advantage. Save the script with a PS1 extension and run from an elevated Powershell command prompt.

If the script is in the current directory, run: .\scriptname.ps1

If the script is not in the current directory, run: <path>\scriptname.ps1

Good luck.



Discussion

No Comment Found