我得到了一個VBS腳本,它生成一個url來從我的網絡上的服務器上下載文件。我現在需要將文件下載到「C:\ rWallpaper \ wallpaper.png」,URL存儲在變量「url」中用VBS下載文件
我喜歡在linux上運行類似wget的工具,只需下載並保存文件到指定的位置。
我得到了一個VBS腳本,它生成一個url來從我的網絡上的服務器上下載文件。我現在需要將文件下載到「C:\ rWallpaper \ wallpaper.png」,URL存儲在變量「url」中用VBS下載文件
我喜歡在linux上運行類似wget的工具,只需下載並保存文件到指定的位置。
您可以使用XMLHTTP下載並利用ADO流寫入二進制數據;
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "http://bla.com/xxx.png", False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "c:\temp\xxx.png", 2 '//overwrite
end with
以上回答扔了錯誤Write to file failed. Code: 800A0BBC
對我來說,但是這工作:
HTTPDownload http://www.emagcloud.com/europeansealing/FSA_ESA_Compression_Packing_Technical_Manual_v3/pubData/source/images/pages/page10.jpg", "C:\"
凡
Sub HTTPDownload(myURL, myPath)
' This Sub downloads the FILE specified in myURL to the path specified in myPath.
'
' myURL must always end with a file name
' myPath may be a directory or a file name; in either case the directory must exist
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
'
' Based on a script found on the Thai Visa forum
' http://www.thaivisa.com/forum/index.php?showtopic=21832
' Standard housekeeping
Dim i, objFile, objFSO, objHTTP, strFile, strMsg
Const ForReading = 1, ForWriting = 2, ForAppending = 8
' Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check if the specified target file or folder exists,
' and build the fully qualified path of the target file
If objFSO.FolderExists(myPath) Then
strFile = objFSO.BuildPath(myPath, Mid(myURL, InStrRev(myURL, "/") + 1))
ElseIf objFSO.FolderExists(Left(myPath, InStrRev(myPath, "\") - 1)) Then
strFile = myPath
Else
WScript.Echo "ERROR: Target folder not found."
Exit Sub
End If
' Create or open the target file
Set objFile = objFSO.OpenTextFile(strFile, ForWriting, True)
' Create an HTTP object
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
' Download the specified URL
objHTTP.Open "GET", myURL, False
objHTTP.Send
' Write the downloaded byte stream to the target file
For i = 1 To LenB(objHTTP.ResponseBody)
objFile.Write Chr(AscB(MidB(objHTTP.ResponseBody, i, 1)))
Next
' Close the target file
objFile.Close()
End Sub
如果接受的答案具體錯誤失敗的問題是權限寫入文件夾,這是不難解決的。 – Lankymart 2017-02-09 09:24:57
你說得對,我使用提供的代碼解決了這個問題。 – 2017-02-09 15:49:13
當文件大小很大時(例如400k pdf,我用20秒下載),您的方法非常緩慢,標記的答案只用2秒來下載相同的文件。 – 2017-06-06 01:32:42
這篇文章很舊,但是在fisrt代碼的答案中的錯誤是,您需要在C:處寫入權限。嘗試在桌面或%temp%,它的工作。
除了亞歷ķ答案,我用下面的,如果它可以幫助別人:
定義對象與文件(在我們的例子圖像)
Set objWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
呼叫下載鏈接
URL = "https://www.grupya.com/public/assets/img/logo.png"
objWinHttp.open "GET", URL, False
objWinHttp.send ""
保存二進制數據到磁盤
SaveBinaryData "c:\temp\my.png",objWinHttp.responseBody
SaveBinaryData功能
Function SaveBinaryData(FileName, Data)
' adTypeText for binary = 1
Const adTypeText = 1
Const adSaveCreateOverWrite = 2
' Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
' Specify stream type - we want To save Data/string data.
BinaryStream.Type = adTypeText
' Open the stream And write binary data To the object
BinaryStream.Open
BinaryStream.Write Data
' Save binary data To disk
BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
End Function
工程就像一個魅力; Toda'a – 2017-10-18 17:04:25
這工作,並幫助了我很多,但你必須指定目標名稱。問題是如果你想使用服務器建議的原始文件名。 – Racky 2013-03-14 15:19:50
@ .send後面(如果服務器認爲是這樣做的話)建議的文件名是在通過hdr = xHttp.getResponseHeader(「Content-Disposition」)提供的'filename ='標記之後' – 2013-03-14 15:30:26