-
-
Save lucasea777/eea1748fe828d89f3755570337e24745 to your computer and use it in GitHub Desktop.
Download a file with Windows Script Host
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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