VBS code that supports breakpoint download

Source: Internet
Author: User

I have introduced the powerful VBScript language before. Today, we will provide a VBS code that supports resumable download.
I have attached the VBS code parsing. Anyone who doesn't know how to use it with Microsoft's SCRIPT56.CHM document. Very simple,
The advantage of VBS is that the Code is easy to understand. Basically, each line of code is displayed in English.
This code is also a supplement to the previously introduced VBS download function.
Old rule: Copy and save as dl. vbe.
However, the difference between the VBS code is not to double-click to run, but to execute it under the CMD command line.

Download function execution format: cscript.exe dl. vbs (target file address)

[Download the WIn32 version of MetaSploit as an example. Enter cscript.exe dl. vbs http://spool.metasploit.com/releases/framework-3.2.exe] in CMD.

It took more than seven minutes to download the 36.7M file, and thunder used 1 minute 50 seconds.

The Code is as follows:Copy codeThe Code is as follows: if (lcase (right (wscript. fullname, 11) = "wscript.exe") then 'determine The Script Host name'
Die ("Script host must be CScript.exe.") 'The Script host is not a CScript, so die'
End if

If wscript. arguments. count <1 then ', there must be at least one parameter'
Die ("Usage: cscript webdl. vbs url [filename]") 'Although Sparrow is small and dirty, Usage cannot be forgotten'
End if

Url = wscript. arguments (0) 'parameter array subscript starts from 0'
If url = "" then die ("URL can't be null.") 'dare to tell me, empty url may not work'
If wscript. arguments. count> 1 then ', first judge whether the number of parameters is greater than 1'
Filename = wscript. arguments (1) 'Access the second parameter'
Else 'get from url if no file name is given'
T = faster Rev (url, "/") 'Get the last "/" location'
If t = 0 or t = len (url) then die ("Can not get filename to save.") 'No "/" or end'
Filename = right (url, len (url)-t) 'get the file name to be saved'
End if
If not left (url, 7) = "http: //" then url = "http: //" & url ", if you forget" http: // "carelessly, add'

Set fso = wscript. createobject ("Scripting. FileSystemObject") 'fso, ASO, and HTTP. Each object must be unique'
Set aso = wscript. createobject ("ADODB. Stream ")
Set http = wscript. createobject ("Microsoft. XMLHTTP ")

If fso. fileexists (filename) then 'determine whether the object to be downloaded already exists'
Start = fso. getfile (filename). size 'exists, with the current file size as the starting position'
Else
Start = 0 'nonexistent, everything starts from scratch'
Fso. createtextfile (filename). close 'create file'
End if

Wscript. stdout. write "Connectting..." 'the beginning of the play'
Current = start 'current position is the start position'
Do
Http. open "GET", url, true 'HTTP is called asynchronously here'
Http. setrequestheader "Range", "bytes =" & start & "-" & cstr (start + 20480) 'here is the secret of resumable upload'
Http. setrequestheader "Content-Type:", "application/octet-stream"
Http. Send' start sending after data packets are constructed'

For I = 1 to 120 'cyclic waiting'
If http. readystate = 3 then showplan () 'status 3 indicates that the data is received and the progress is displayed'
If http. readystate = 4 then exit for 'status 4 indicates the data is accepted successfully'
Wscript. sleep 500 'Wait for 500ms'
Next
If not http. readystate = 4 then die ("Timeout.") '20 k hasn't been finished in 1 minute? Timeout! '
If http. status> 299 then die ("Error:" & http. status & "& http. statustext) 'Isn't it, and an Error occurs? '
If not http. status = 206 then die ("Server Not Support Partial Content.") 'the Server does not Support resumable upload'

Aso. type = 1 'data stream type set to byte'
Aso. open
Aso. loadfromfile filename 'open file'
Aso. position = start 'set the initial position of the file pointer'
Aso. write http. responsebody 'write Data'
Aso. savetofile filename, 2 'overwrite'
Aso. close

Range = http. getresponseheader ("Content-Range") 'Get "Content-Range"' in the http Header "'
If range = "" then die ("Can not get range.") 'You don't know if the download is complete'
Temp = mid (range, instr (range, "-") + 1) 'content-Range is similar to 123-456/789'
Current = clng (left (temp, instr (temp, "/")-1) '2014 is the start position and 123 is the end position'
Total = clng (mid (temp, instr (temp, "/") + 1) '100 is the total number of bytes of the file'
If total-current = 1 then exit do 'the end position is 1 less than the total size, indicating that the transfer is completed'
Start = start + 20480 'otherwise download 20 k'
Loop while true

Wscript. echo chr (13) & "Download (" & total & ") Done." 'after downloading, the total number of bytes is displayed'

Function die (msg) 'function name comes from Perl built-in function die'
Wscript. echo msg 'last words ^_^'
Wscript. quit 'Go to see Marx'
End function

Function showplan () 'display download progress'
If I mod 3 = 0 then c = "/" 'simple dynamic Effect'
If I mod 3 = 1 then c = "-"
If I mod 3 = 2 then c = "\"
Wscript. stdout. write chr (13) & "Download (" & current & ")" & c & chr (8) '13' ASCII code is returned to the beginning of the line, and 8'
End function

The above is the complete download code that supports breakpoint written with VBS. It is very suitable for companies that prohibit XunLei and Flashget. Speed is a problem. Multi-threaded download is required.

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.