使用代理从 URL 批量下载 Windows 文件 [英] Windows batch download file from URL using a proxy
问题描述
我每天早上必须安排从 Web 下载 xls 文件并保存到本地文件夹中.我想制作一个 CMD 文件,然后使用 Windows 调度程序.
I have to schelude the download of a xls file from the Web and save into a local folder every morning. I would like to make a CMD file and then use the Windows scheduler.
该文件始终具有相同的 URL,例如 www.example.com/myfile.xls.
The file has always the same URL, something like www.example.com/myfile.xls.
我找到了一些教程并截取了有关它的代码,但我的问题是我需要使用代理服务器(proxy.mycompany.com"和端口1234").
I've found some tutorial and snipped of code about it but my problem is that I need to use a proxy server ("proxy.mycompany.com" and port "1234").
是否可以使用 CMD 文件来做到这一点,或者我需要转向其他一些解决方案(VB.net,...)?
Is it possible to do that with a CMD file or I need to move to some other solutions (VB.net, ...)?
推荐答案
我建议使用嵌入在 .cmd 中的 JScript
I would advise to use JScript embedded in .cmd
@set @tmpvar=1 /*
@echo off
echo Downloading...
call :download "http://download.qt.io/official_releases/jom/jom.zip" || echo Failed
echo Downloading using proxy...
call :download "http://download.qt.io/official_releases/jom/jom_1_1_1.zip" "206.128.191.77:8008" || echo Failed
goto :EOF
rem Function :download
rem %1 - URL
rem %2 - [Proxy]
rem %3 - [file name]
:download
cscript /nologo /e:jscript "%~f0" %*
exit /b %ERRORLEVEL%
*/
function getFileName(uri) {
var re = /\/([^?/]+)(?:\?.+)?$/;
var match = re.exec(uri);
return match != null ? match[1] : "output";
}
try {
var Source = WScript.Arguments.Item(0);
var Proxy = WScript.Arguments.Length > 1 ? WScript.Arguments.Item(1) : "";
var Target = WScript.Arguments.Length > 2 ? WScript.Arguments.Item(2) : getFileName(Source);
var Object = WScript.CreateObject('MSXML2.ServerXMLHTTP');
if (Proxy.length > 0) {
Object.setProxy(2/*SXH_PROXY_SET_PROXY*/, Proxy, "");
}
Object.open('GET', Source, false);
Object.send();
if (Object.status != 200) {
WScript.Echo('Error:' + Object.status);
WScript.Echo(Object.statusText);
WScript.Quit(1);
}
var File = WScript.CreateObject('Scripting.FileSystemObject');
if (File.FileExists(Target)) {
File.DeleteFile(Target);
}
var Stream = WScript.CreateObject('ADODB.Stream');
Stream.Open();
Stream.Type = 1/*adTypeBinary*/;
Stream.Write(Object.responseBody);
Stream.Position = 0;
Stream.SaveToFile(Target, 2/*adSaveCreateOverWrite*/);
Stream.Close();
} catch (e) {
WScript.Echo("--------------------");
WScript.Echo("Error " + (e.number & 0xFFFF) + "\r\n " + e.description.replace(/[\r\n]*$/, "\r\n"));
for (var i = 0; i < WScript.Arguments.length; ++i) {
WScript.Echo(" arg" + (i+1) + ": " + WScript.Arguments(i));
}
WScript.Echo("--------------------");
WScript.Quit(1);
}
这篇关于使用代理从 URL 批量下载 Windows 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!