2017-04-06 21 views
1

我必須從Web上下載一個xls文件並每天早上保存到本地文件夾中。 我想製作一個CMD文件,然後使用Windows調度程序。使用代理服務器從URL進行批量下載文件

該文件始終具有相同的網址,如www.example.com/myfile.xls

我發現了一些教程,並關於它的代碼剪斷,但我的問題是,我需要使用代理服務器(「proxy.mycompany.com」和端口「1234」)。

是否可以用CMD文件做到這一點,或者我需要移動到其他解決方案(VB.net,...)?

+0

檢查[這](http://stackoverflow.com/questions/28043589/how-can-i-compress-zip-and-uncompress -unzip-files-and-folders-with-bat)一些工具可以選擇設置代理設置。 – npocmaka

回答

1

我會建議使用JScript中嵌入。加利福尼亞

@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); 
} 
相關問題