Skip to content

Instantly share code, notes, and snippets.

@flisboac
Created August 21, 2013 22:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flisboac/6301123 to your computer and use it in GitHub Desktop.
Save flisboac/6301123 to your computer and use it in GitHub Desktop.
Download file using JScript. Not tested (yet), because of work's proxy policies and Stupid Windows!
var fs = new ActiveXObject("Scripting.FileSystemObject");
var shell = new ActiveXObject("WScript.Shell");
var url = WScript.Arguments(0);
var to;
try {
to = WScript.Arguments(1);
} catch(err) {
to = null;
}
if (!to) {
to = shell.CurrentDirectory;
}
if (download(url, to)) {
WScript.Quit(0);
} else {
WScript.Quit(1);
}
function download(url, to, opts) {
opts = opts || {};
var fs = new ActiveXObject("Scripting.FileSystemObject");
var http = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
var path = fs.getAbsolutePathName(to);
var filename = /([^\/]*)$/.exec(url)[1];
var filepath = fs.BuildPath(path, filename);
if (!fs.FolderExists(path)) {
// Criar pasta se não existir
fs.CreateFolder(path);
}
// Gets the file
http.Open("GET", url, false);
http.Send();
WScript.Echo("`url`: " + url
+ "\n`to`: " + to
+ "\npath: " + path
+ "\nfilename: " + filename
+ "\nfilepath: " + filepath
+ "\nSTATUS: " + http.Status);
if (http.Status >= 200 && http.Status < 300) {
var bytes = http.ResponseBody;
var size = bytes.length;
var file = fs.OpenTextFile(filepath, 2, true);
for (var i = 0; i < size; i++) {
file.Write(bytes[i]);
}
file.Close();
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment