Rob van der Woude's Scripting Pages

Fileaze banner

VBScript Scripting Techniques > Network > Download Files

Download Files


VBScript Code:
HTTPDownload "", "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
' Based on a script found on the Thai Visa forum

    ' 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
        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

    ' 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 ) ) )

    ' Close the target file
    objFile.Close( )
End Sub
Windows version: 2000 SP3, XP, Server 2003, or Vista
Network: any
Client software: Internet Explorer 5.01
Script Engine: any
Summarized: Works in Windows 2000 SP3 or later.
Should work in Windows 95, 98, ME, or NT 4 with Internet Explorer 5.01 or later.
[Back to the top of this page]
VBScript Code:
Download "", "C:\"

Sub Download( myFileURL, myDestFile )
' This function uses's X-HTTP component to download a file
' Arguments:
' myFileURL  [string] the URL of the file to be downloaded
' myDestFile [string] the fully qualified path of the downloaded "target" file
' Written by Rob van der Woude
' The X-HTTP component is available at:
' For more information on available functionality read:
    Dim objHTTP
    Set objHTTP = CreateObject("XStandard.HTTP")
    objHTTP.Get myFileURL
    objHTTP.SaveResponseToFile myDestFile
    Set objHTTP = Nothing
End Sub
Windows version: any
Network: any
Client software: X-HTTP component
Script Engine: any
Summarized: Works in any Windows version with the X-HTTP component installed.
[Back to the top of this page]
Note: For large files, WinHTTP may prove far too slow.
Better use this MSXML2.XMLHTTP/ADODB based code by Wataru Uda instead.



page last uploaded: 2015-08-24, 21:17