Skip to content

Instantly share code, notes, and snippets.

@lucasea777
Forked from duncansmart/httpget.js
Created October 7, 2016 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasea777/eea1748fe828d89f3755570337e24745 to your computer and use it in GitHub Desktop.
Save lucasea777/eea1748fe828d89f3755570337e24745 to your computer and use it in GitHub Desktop.
Download a file with Windows Script Host
// httpget.js: download a file (Windows Script Host)
// usage: cscript httpget.js <url> <file>
(function() {
if (WScript.Arguments.Length != 2) {
WScript.Echo("Usage: httpget.js <url> <file>")
WScript.Quit(1)
}
var url = WScript.Arguments(0)
var filepath = WScript.Arguments(1)
var xhr = new ActiveXObject("MSXML2.XMLHTTP")
xhr.open("GET", url, false)
xhr.send()
if (xhr.Status == 200) {
var fso = new ActiveXObject("Scripting.FileSystemObject")
if (fso.FileExists(filepath))
fso.DeleteFile(filepath)
var stream = new ActiveXObject("ADODB.Stream")
stream.Open()
stream.Type = 1 //adTypeBinary
stream.Write(xhr.ResponseBody)
stream.Position = 0
stream.SaveToFile(filepath)
stream.Close()
}
else {
WScript.Echo("Error: HTTP " + xhr.status + " "+ xhr.statusText)
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment