-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadFile.vbs
More file actions
133 lines (101 loc) · 4.24 KB
/
DownloadFile.vbs
File metadata and controls
133 lines (101 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
' ---------------------------------
' Download a file from a website
'
' Author: Alex Hedley - 599CD.com
' Created: 3rd November 2012
' Updated: 9th September 2022
' ---------------------------------
' If you wanted the same thing everytime then you just need this one line of code.
' It will overwrite the file if it already exists
'download "http://599cd.s3-website-us-east-1.amazonaws.com/StudentFiles/Access/Access-2010-B1.zip", "D:\599CD\Sample\Access-2010-B1.zip"
'InputBox( prompt [, title] [, default] [, xpos] [, ypos] [, helpfile] [, context] )
' If you wish to download the file more than once prompt for an amount of times to loop
Dim TimesToDownload
TimesToDownload = InputBox ("Loop for ? times", "Loop For?", "3")
If TimesToDownload = "" Then
Msgbox "You didn't chose a number.", vbCritical, "Quitting"
WScript.Quit
End If
For i = 1 to TimesToDownload
DownloadFile(i)
Next
' If you only wish to download it once you could comment out the above code and uncomment the below line.
' Sending an empty string "" will keep the filename as "Test.exe"
'downloadfile ""
Msgbox "All Complete", vbInformation, "Complete"
' ------------------------------
' Script to choose a FileName
' Calls the "download" function
' ------------------------------
Function DownloadFile(i)
' Don't need a prompt for the Website as this is static, if you did you could do something like this:
'Dim Website
'InputBox( prompt [, title] [, default] [, xpos] [, ypos] [, helpfile] [, context] )
'Website = InputBox ("Choose a Website Address" & vbNewLine & "and File to download:", "Website", "http://599cd.s3-website-us-east-1.amazonaws.com/StudentFiles/Access/Access-2010-B1.zip")
Website = "http://599cd.s3-website-us-east-1.amazonaws.com/StudentFiles/Access/Access-2010-B1.zip"
' Static FilePath
Dim FilePath
FilePath = "D:\599CD\Sample\"
Dim FileName
' Increment by the loop "i"
'InputBox( prompt [, title] [, default] [, xpos] [, ypos] [, helpfile] [, context] )
FileName = InputBox ("Path: " & FilePath & vbNewLine & vbNewLine & "Choose a FileName" _
, "FileName and Location", "Test" & i & ".exe")
' Join the FilePath to the FileName
Dim File
File = FilePath & FileName
' Check that the User has chosen both
'If IsNull(Website) Or IsNull(FileName) Then
If Website = "" Or FileName = "" Then
'MsgBox( prompt [, buttons] [, title] [, helpfile, context] )
MsgBox "You haven't chosen a Website or FileName", vbExclamation, "Error"
WScript.Quit
Else
' Call the "download" function with the website and file
download Website, File
' Inform the user it is complete
'MsgBox( prompt [, buttons] [, title] [, helpfile, context] )
Msgbox "Complete", vbInformation, "Complete"
End If
End Function
' --------------------------
' Script to download a File
' --------------------------
function download(sFileURL, sLocation)
'create xmlhttp object
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
'get the remote file
objXMLHTTP.open "GET", sFileURL, false
'send the request
objXMLHTTP.send()
'wait until the data has downloaded successfully
do until objXMLHTTP.Status = 200 : wscript.sleep(1000) : loop
'if the data has downloaded sucessfully
If objXMLHTTP.Status = 200 Then
'create binary stream object
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
'adTypeBinary
objADOStream.Type = 1
objADOStream.Write objXMLHTTP.ResponseBody
'Set the stream position to the start
objADOStream.Position = 0
'create file system object to allow the script to check for an existing file
Set objFSO = Createobject("Scripting.FileSystemObject")
'check if the file exists, if it exists then delete it
If objFSO.Fileexists(sLocation) Then objFSO.DeleteFile sLocation
'destroy file system object
Set objFSO = Nothing
'save the ado stream to a file
objADOStream.SaveToFile sLocation
'close the ado stream
objADOStream.Close
'destroy the ado stream object
Set objADOStream = Nothing
'end object downloaded successfully
End if
'destroy xml http object
Set objXMLHTTP = Nothing
End function
' Sample
'download "http://remote-location-of-file", "C:\name-of-file-and-extension"