Reputation: 2714
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.
The file has always the same URL, something like www.example.com/myfile.xls.
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").
Is it possible to do that with a CMD file or I need to move to some other solutions (VB.net, ...)?
Upvotes: 2
Views: 1527
Reputation: 3180
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);
}
Upvotes: 2