VBS Downloader / updater无法正常工作

时间:2015-06-01 13:54:27

标签: vbscript

感谢@Hackoo获取以下代码。我真的不知道它有什么问题,但它似乎并不想下载文件(http://mollernielsen.eu/AutomaticShutdown/test.bat),这对我来说并没有多大意义。

path = "http://mollernielsen.eu/AutomaticShutdown/test.bat"
pos = InStrRev(path, "/") +1
Const DownloadDest = "http://mollernielsen.eu/AutomaticShutdown/test.bat"
LocalFile = Mid(path, pos)
Const webUser = "admin"
Const webPass = "admin"
Const DownloadType = "binary"
dim strURL

function getit()
 dim xmlhttp

 set xmlhttp=createobject("MSXML2.XMLHTTP.3.0")
 'xmlhttp.SetOption 2, 13056 'If https -) Ignorer toutes les erreurs SSL
 strURL = DownloadDest

 'Pour l'authentification de base, utilisez la liste ci-dessous, ainsi que les variables + d'utilisateurs? laisser passer
 'xmlhttp.Open "GET", strURL, false, WebUser, WebPass
 xmlhttp.Open "GET", strURL, false

 xmlhttp.Send

 If xmlhttp.Status = 200 Then
   Dim objStream
   set objStream = CreateObject("ADODB.Stream")
   objStream.Type = 1 'adTypeBinary
   objStream.Open
   objStream.Write xmlhttp.responseBody
   objStream.Close
   set objStream = Nothing
 End If


 set xmlhttp=Nothing
End function

getit()

我不知道代码有什么问题,它似乎已经启动,但没有保存文件且没有错误。

2 个答案:

答案 0 :(得分:1)

脚本中没有调用实际的objStream.SaveToFile "test.bat", 2 方法,但应该有。流正在保存到内存中的对象,但从未写入磁盘。坚持上面GROUP BY

HAVING

答案 1 :(得分:1)

试试这样:

Option Explicit
Dim URL
URL = "http://mollernielsen.eu/AutomaticShutdown/test.bat"
Call DownloadingFile(URL)
'*************************************************************************************************
Sub DownloadingFile(URL)
    On Error Resume Next
    Dim objFSO,Ws,objXMLHTTP,PathScript,Tab,strHDLocation,objADOStream,File,ProtocoleHTTP
    Set objFSO = Createobject("Scripting.FileSystemObject")
    Set Ws = CreateObject("wscript.Shell")
    PathScript = objFSO.GetParentFolderName(wscript.ScriptFullName) 'Path of this Vbscript
    ProtocoleHTTP = "http://"
    If URL = "" Then WScript.Quit
    If Left(URL,7) <> ProtocoleHTTP Then
        URL = ProtocoleHTTP & URL
    End if
    Tab = split(url,"/")
    File =  Tab(UBound(Tab))
    File = Replace(File,"%20"," ")
    File = Replace(File,"%28","(")
    File = Replace(File,"%29",")")
    Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.3.0")
    strHDLocation = PathScript & "\" & File
    objXMLHTTP.open "GET",URL,false
    objXMLHTTP.send()
    If Err.number <> 0 or objXMLHTTP.Status <> 200 Then
        MsgBox err.description & objXMLHTTP.Status,16,err.description & objXMLHTTP.Status
        Exit Sub
    Else
        If objXMLHTTP.Status = 200 Then
            strHDLocation = PathScript & "\" & File
            Set objADOStream = CreateObject("ADODB.Stream")
            objADOStream.Open
            objADOStream.Type = 1 'adTypeBinary
            objADOStream.Write objXMLHTTP.ResponseBody
            objADOStream.Position = 0    'Set the stream position to the start
            objADOStream.SaveToFile strHDLocation,2
            objADOStream.Close
            Set objADOStream = Nothing
        End If
    End if
    Set objXMLHTTP = Nothing
    ws.Popup "The Download of " & Dblquote(File) & " is finished ! ","5","The Download of " & Dblquote(File) & " is finished !" ,64
End Sub
'**********************************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************