VBS multithreaded Download Implementation code _vbs

Source: Internet
Author: User
Say or idle to practicing, the preliminary realization of their own thought of "multithreading" download. (As for is not multiple threads, you can refer to the 12 floor link)
To avoid redundancy, some error checking is saved. I do not think much practical use, interested in the brothers to learn to discuss Bai. You are welcome to correct me:

Copy Code code as follows:

' By Wankoilz

Url=inputbox ("Enter full download Address:")
Threadcount=inputbox ("Enter the number of threads (no more than 10, too much is redundant)")
Filename=getfilename (URL)
Filepath=getfilepath (Wscript.scriptfullname)
Set ohttp=createobject ("Msxml2.xmlhttp")
Set ado=createobject ("ADODB.stream")
Set fso=createobject ("Scripting.FileSystemObject")
Ado. Type=1
Ado. Mode=3
Ado. Open
Ohttp.open "Head", Url,true
Do While ohttp.readystate<>4
Wscript.Sleep 200
' Get file size
Filesize=ohttp.getresponseheader ("Content-length")
' Create a temporary file of the same size as the download file for the following ADO fragment rewrite
Fso. CreateTextFile (filepath& "Tmpfile", True,false). Write (Space (fileSize))
Ado. LoadFromFile (filepath& "Tmpfile")

Blocksize=fix (filesize/threadcount): remaindersize=filesize-threadcount*blocksize
' defines an array containing Msxml2.xmlhttp objects-the number of members is the number of threads
' Direct Dim array name (variable name) It's not going to work, here's a workaround with execute
Execute ("Dim arrhttp" ("&upbound&"))
For i=0 to UBound (arrhttp)
Endpos= (i+1) *blocksize-1
If I=ubound (arrhttp) Then endpos=endpos+remaindersize
Set arrhttp (i) =createobject ("Msxml2.xmlhttp")
Arrhttp (i). Open "Get", url,true
' Segmented download
Arrhttp (i) setrequestheader "Range", "bytes=" &startpos& "-" &endpos
Arrhttp (i). Send
Wscript.Sleep 200
For i=0 to UBound (arrhttp)
If arrhttp (i). readystate=4 Then
' Write to the appropriate location of the temporary file whenever a thread has been downloaded
Ado. Position=i*blocksize
MsgBox "Thread" &i& "Download complete!" "
Ado. Write arrhttp (i) responsebody
Arrhttp (i) abort
End If
If Complete=ubound (arrhttp) +1 Then Exit do
If timeout=5*30 Then
' Set according to file size
MsgBox "30 Seconds Timeout!" "
End If
If FSO. FileExists (filepath&filename) Then fso. DeleteFile (Filepath&filename)
Fso. DeleteFile (filepath& "Tmpfile")
Ado. SaveToFile (Filepath&filename)
MsgBox "File Download complete!" "

Function getfilename (URL)
Arrtmp=split (URL, "/")
Getfilename=arrtmp (UBound (arrtmp))
End Function

Function GetFilePath (FullName)
Arrtmp=split (fullname, "\")
For i=0 to UBound (arrtmp)-1
Getfilepath=getfilepath&arrtmp (i) & "\"
End Function

Test Download Address:
Copy Code code as follows:


VBS to implement multithreading complement

Someone emailed me today asking me a question:

Would you like to ask if the InputBox function in VBS is closed?
How do I close the input box if I can? Thank you very much.

At first glance this is not possible, because the InputBox function itself does not have a time-out parameter, and the program waits until InputBox returns to continue, and the following statement cannot be executed before the InputBox returns.

If VBS can implement multithreading in high-level languages ... Unfortunately, VBS is not possible to implement multithreading, but it can be simulated with the SetTimeout method.

Copy Code code as follows:

Dim IE
Set IE = CreateObject ("Internetexplorer.application")
Ie. Navigate "About:blank"
Set window = IE. Document.parentwindow
id = window.settimeout (GetRef ("On_timeout"), 3000, "VBScript")
Name = InputBox ("Please enter your name", "InputBox Timeout")
Window.cleartimeout ID
If name <> "Then MsgBox" Hello, "& name
Ie. Quit

' By Demon
' http://demon.tw

Sub On_timeout ()
Dim WshShell
Set WshShell = CreateObject ("WScript. Shell ")
Wshshell.sendkeys "{ESC}"
End Sub

Use the SetTimeout method to set the 3-second timeout, 3 seconds after the SendKeys method to send the ESC key to end the InputBox. Of course, using SendKeys is very unreliable, I usually rarely use the SendKeys method, because it makes too many assumptions, in case InputBox is not activated window? This is only for the simple application of the SendKeys, can be replaced by the end of the script itself.

Similarly, if you want to implement the timer event in VB in the VBS, you can use the SetInterval method, I will not write examples, I look at the document.

Reference Links: SetTimeout Method (window, window constructor)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.