vbs http download slow

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 397
Joined: 28 Jun 2010 03:46

vbs http download slow

#1 Post by miskox » 23 Jan 2021 14:23

I found this HTTP download .vbs script at www.robvanderwoude.com.

main.cmd
@cscript /NOLOGO /E:VBS HTTPdownload.vbs "http://www.kockarna.si/slike.zip" "."
HTTPdownload.vbs (I added the first IF statement - maybe it should be improved (I have no experience with .vbs)) .
if WScript.Arguments.Count <> 2 then
WScript.Echo "Missing parameters"
else
HTTPDownload WScript.Arguments(0), WScript.Arguments(1)
end if


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
FIle in the .cmd (slike.zip) is located on my website - it is safe to download. It contains some .jpg files of a flat. (size 36 Mbytes).

The problem I have is: this .vbs download is veeeeeeeeeery slow. If I use wget.exe it downloads with the expected speed.

What I noticed with this .vbs: output file is created with the size of zero bytes (of course) - size does not change for a while (actual download of the file occurs). Then size starts to increment very slowly and CPU usage is 100% (cscript.exe) - this means that the last FOR-NEXT loop is the problem.

Any ideas/alternatives? Do you have the same problem?

I would like to use this (or similar) download .vbs script in my .cmd (I don't want to use wget.exe).

Thanks.
Saso
Last edited by miskox on 23 Jan 2021 14:41, edited 1 time in total.

aGerman
Expert
Posts: 4195
Joined: 22 Jan 2010 18:01
Location: Germany

Re: vbs http download slow

#2 Post by aGerman » 23 Jan 2021 14:32

It seems to write byte by byte. I think I have something in my collection which uses an ADO stream object. I just have to find it ...

Steffen

miskox
Posts: 397
Joined: 28 Jun 2010 03:46

Re: vbs http download slow

#3 Post by miskox » 23 Jan 2021 14:38

Please do try to find it. Any other solutions would be great (I would really prefer to be backward compatible to XP).

Thanks.
Saso

aGerman
Expert
Posts: 4195
Joined: 22 Jan 2010 18:01
Location: Germany

Re: vbs http download slow

#4 Post by aGerman » 23 Jan 2021 14:46

It's not too complicated. You don't need the OpenTextFile and stuff, and you don't need the For-Next loop.
Just capture the responseBody in a stream object and save it. Like that:

Code: Select all

Dim objADOStream
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Type = 1
objADOStream.Mode = 3
objADOStream.Open
objADOStream.Write objHTTP.responseBody
objADOStream.SaveToFile strFile, 2
objADOStream.Close
Just get back to me if you are not familiar with VBS. I'm certainly able to overhaul your script.

// EDIT
tested VBScript:

Code: Select all

Option Explicit

If WScript.Arguments.Count <> 2 Then
	WScript.Echo "Missing parameters"
Else
	HTTPDownload WScript.Arguments(0), WScript.Arguments(1) 
End If


Sub HTTPDownload( myURL, myPath )
    Dim objADOStream, objFSO, objHTTP, strFile, strMsg

    ' 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 an HTTP object
    Set objHTTP = CreateObject( "MSXML2.ServerXMLHTTP" )

    ' Download the specified URL
    objHTTP.open "GET", myURL, False
    objHTTP.setRequestHeader "Cache-Control", "private, no-cache, no-store, must-revalidate"
    objHTTP.setRequestHeader "Pragma", "no-cache"
    objHTTP.send

    ' Write the downloaded byte stream to an ADO stream, and save it in the target file
    Set objADOStream = CreateObject( "ADODB.Stream" )
    objADOStream.Type = 1
    objADOStream.Mode = 3
    objADOStream.Open
    objADOStream.Write objHTTP.responseBody
    objADOStream.SaveToFile strFile, 2
    objADOStream.Close
End Sub

miskox
Posts: 397
Joined: 28 Jun 2010 03:46

Re: vbs http download slow

#5 Post by miskox » 23 Jan 2021 15:18

Thank you Steffen!

As you probably noticed: I have no experience with .vbs. Your solution is perfect - download is as fast as possible.

Thank you again.
Saso

Post Reply