使用VBS下载文件

时间:2010-06-04 10:08:41

标签: vbscript

我有一个VBS脚本,它生成一个URL来从我网络上的服务器下载文件。我现在需要将文件下载到“C:\ rWallpaper \ wallpaper.png”,URL存储在变量“url”中

我希望它能像linux上的wget一样工作,只需下载并将文件保存到指定位置。

6 个答案:

答案 0 :(得分:36)

您可以使用XMLHTTP下载并利用ADO流来编写二进制数据;

dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "http://example.com/someimage.png", False
xHttp.Send

with bStrm
    .type = 1 '//binary
    .open
    .write xHttp.responseBody
    .savetofile "c:\temp\someimage.png", 2 '//overwrite
end with

答案 1 :(得分:6)

上面的回答为我抛出了错误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

答案 2 :(得分:4)

除了Alex K回答之外,如果有人帮助我,我会使用以下内容:

定义对象

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

答案 3 :(得分:0)

This post is old, but the error in the fisrt code answer is that you need privileges to write at C:. Try at desktop or %temp%, it works.

答案 4 :(得分:0)

我发现映射驱动器是一个不错的方法

   dim Z
   Z = "http:// .... " '(base url)
   Set FSO = CreateObject("Scripting.Filesystemobject")
    Set objNetwork = CreateObject("WScript.Network") 
    '## This is for network drives
    
    If FSO.driveExists ("Z:") then 
        objNetwork.RemoveNetworkDrive "Z:", True, True
    End If      

    '## for adding
    'Set objNetwork = CreateObject("WScript.Network")
    objNetwork.MapNetworkDrive "Z:" , Z

从那里,您可以将 url 的其余部分定义到您的文件中

SourceFileName = "Z:\" & ...

和目标文件名

DestinFileName = "..."

然后你就可以使用

 FSO.CopyFile SourceFileName, DestinFileName, True

答案 5 :(得分:-1)

尝试:

CreateObject("WScript.Shell").Run "curl -o "Path and filename"