VBScript – Check to See if Web File exists, Download It

email me

Dim varHTTP, varBinaryString, varFileName, varLink

set objShell = CreateObject("WScript.Shell")
 
Set varHTTP = CreateObject("Microsoft.XMLHTTP")
Set varBinaryString = CreateObject("Adodb.Stream")

varFileName = "Foo.exe"
varLink = "http://TheWebURL/" & varFileName
varHTTP.Open "GET", varLink, False
varHTTP.Send


'Sequencing...
CheckFile()
DownloadFile()

'add other stuff to do here


sub CheckFile()
	Select Case Cint(varHTTP.status)
		Case 200, 202, 302 
			'it exists
			msgbox "File exists!"
			Exit Sub
		Case Else
			'does not exist			
			msgbox "File does not exist!"
			WScript.quit
	End Select
end sub

sub DownloadFile()
		With varBinaryString
			.Type = 1 'my type has been set to binary
			.Open
			.Write varHTTP.responseBody
			.SaveToFile ".\" & varFileName, 2 'if exist, overwrite
		End With
		varBinaryString.close
		msgbox "Download complete!"
end sub

 
Notes

200 – OK (standard successful http request)
202 – Accepted (request accepted for processing, but not completed)
302 – Found (via redirection)